C 如何读取文本文件';s内容并返回我指定的两个字符串之间的字符串?
下面是C 如何读取文本文件';s内容并返回我指定的两个字符串之间的字符串?,c,C,下面是example.txt: ##TITLE My program # 1 # 2 ## 到目前为止,我有一个函数,它读取整个文件并以char*格式返回其内容 char *getTextBlock(const char *filename, char *textBlockLabel){ char *fileContents; long fileSize; FILE *fp= fopen(filename,"r"); fseek(fp, 0, SEEK_E
example.txt
:
##TITLE
My program
# 1
# 2
##
到目前为止,我有一个函数,它读取整个文件并以char*
格式返回其内容
char *getTextBlock(const char *filename, char *textBlockLabel){
char *fileContents;
long fileSize;
FILE *fp= fopen(filename,"r");
fseek(fp, 0, SEEK_END);
fileSize= ftell(fp);
rewind(fp);
fileContents= malloc(fileSize * sizeof(char));
fread(fileContents, sizeof(char), fileSize, fp);
fclose(fp);
return fileContents;
}
当我运行这个程序时,这个函数将是getTextBlock(“example.txt”,“TITLE”)代码>
我想在example.txt
中获取并返回##TITLE
和##
之间的所有内容
在这种情况下,这意味着:
My program
# 1
# 2
我该怎么做呢?问题是一旦你把文件内容变成字符串,你就可以做这件事了
char *begin,*end;
begin= strstr( s, pattern) // ##TITLE
char *op = NULL;
if ( begin !=NULL )
{
begin += strlen( pattern);
end = strstr( begin, pattern1); // ##
if (end!=NULL )
{
op= malloc( end- begin+ 1 );
if(op == NULL)
{
printf("%s","ERROR in Allocation");
// if you use in function return or show message
}
else
{
memcpy( op, begin, end- begin);
op[end - begin] = '\0';
//print op or what you want
}
}
}
else
{
// first pattern not found. return empty string or show message
}
来自莫斯科弗拉德的大部分帮助。
这是他的。从这里我几个月前就用过了..并储存在我的机器里。现在我又用了
守则的解释
这段代码完全按照要求我们执行任务时所做的那样执行
- 因此,首先,我们试图从包含文件包含的所有内容的字符串中找出第一个“TITLE###”
- 如果我们没有找到它,那么程序将不打印任何内容,或者可能返回一个空字符串
- 如果第一个
strstr
返回notNULL
,则我们找到了它。我们将再次开始搜索。但是我们从哪里开始呢?我们需要从第一个模式出现的末尾开始,否则我们将从标题#####
中找到我们不想要的本身
- 所以我们正确地设置了指针,然后再次搜索第二个模式
- 如果找到,我们将只分配一个大小为
end begin+1
的字符串,该额外空间1
用于保留\0
或字符串结束标记
- 我们需要检查
malloc
的返回值是否成功
注意:最好使用NUL
来终止字符串,我的代码假设是这样的。只需将\0
放在收集文件所有内容的字符串末尾 我的解决方案:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getTextBlock(const char *filename, char *textBlockLabel){
char *fileContents;
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen (filename , "r" );
if (pFile==NULL) {
fputs ("File error",stderr);
return NULL;
}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {
fputs ("Memory error",stderr);
return NULL;
}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != (unsigned long)lSize) {
fputs ("Reading error",stderr);
return NULL;
}
fileContents = strstr(buffer, textBlockLabel);
if(fileContents!= NULL)
fileContents+= strlen(textBlockLabel);
fclose(pFile);
free (buffer);
return fileContents;
}
int main()
{
char * content = getTextBlock("example.txt","##TITLE");
if(content != NULL)
printf("%s", content);
return 0;
}
有两种模式:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getTextBlock(const char *filename, char *start, char *stop){
char *fileContents;
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen (filename , "r" );
if (pFile==NULL) {
fputs ("File error",stderr);
return NULL;
}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {
fputs ("Memory error",stderr);
return NULL;
}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != (unsigned long)lSize) {
fputs ("Reading error",stderr);
return NULL;
}
fileContents = strstr(buffer, start);
if(fileContents!= NULL)
fileContents+= strlen(start);
char *end = strstr(fileContents, stop);
if(end == NULL)
return NULL;
end[0]= '\0';
fclose(pFile);
free (buffer);
return fileContents;
}
int main()
{
char * content = getTextBlock("example.txt","##TITLE", "##");
if(content != NULL)
printf("%s", content);
return 0;
}
我如何做到这一点?
-->请展示您迄今为止的研究/调试工作。请先阅读第页。由于“##TITLE”
不包括行尾,我希望输出从后面的##TITLE
行尾开始。因此,输出将是“\n我的程序\n#1\n…”“
在您的函数中,我没有看到第二种模式(“##”)您是否特别希望返回行(相对于单词或字符)?换句话说,如果该示例文件的第一个类似项是##TITLE SomeText
,那么“SomeText”
应该是输出的一部分吗?您使用短语TextBlock
表明您希望一次在线上操作。如果是这样,最好将文件拆分为一行字符串数组,使信息单元成为“文本行”;目前,这是“性格”。(另外,getTextBlock
函数可能需要第三个参数:类似于textBlockEnd
)奇怪的是,为什么要使用类型unsigned long lSize;。。。。lSize=ftell(pFile)
当ftell()
返回long
?@RoadRunner时,对我来说它是唯一的自定义:(type*)malloc(sizeof(type)*length);不要使用已被释放的区域。这是未定义的行为。要使strstr
在所有情况下都能正常工作,您的缓冲区应以null结尾。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getTextBlock(const char *filename, char *start, char *stop){
char *fileContents;
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen (filename , "r" );
if (pFile==NULL) {
fputs ("File error",stderr);
return NULL;
}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {
fputs ("Memory error",stderr);
return NULL;
}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != (unsigned long)lSize) {
fputs ("Reading error",stderr);
return NULL;
}
fileContents = strstr(buffer, start);
if(fileContents!= NULL)
fileContents+= strlen(start);
char *end = strstr(fileContents, stop);
if(end == NULL)
return NULL;
end[0]= '\0';
fclose(pFile);
free (buffer);
return fileContents;
}
int main()
{
char * content = getTextBlock("example.txt","##TITLE", "##");
if(content != NULL)
printf("%s", content);
return 0;
}
My program
# 1
# 2