【发布时间】:2015-05-05 03:08:11
【问题描述】:
我正在尝试将文件中的一行重复附加到名为行的字符串中。当我尝试打印这些行时,我的代码运行良好,但由于我必须解析信息,所以我需要存储它。
int main(int argc, char *argv[])
{
// Check for arguments and file pointer omitted
FILE *f = fopen(argv[1], "r");
char *times;
int i = 0;
for (i = 0; i < 2000; i++)
{
char line[80];
if (fgets(line, 80, f) == NULL)
break;
//I want every line with the text "</time> to be added to string times
if(strstr(line, "</time>"))
{
times = strcat(times, line); //This line is my problem
}
}
printf(times);
fclose(f);
return 0;
}
【问题讨论】:
-
您阅读过strcat 的文档吗? “目标字符串必须有足够的空间存放结果”
-
而且也必须被
nul终止。