-2
我试图从txt文件中取出每个单词,然后将其放入数组中。我的代码没有问题从文件中取出每个单词并将其保存为一个字符串。但是,当我尝试将字符串放入一个数组并将其打印出来并仅打印出最后几行时,它全部失真。将字符串放入数组c时出现扭曲
这里是我的代码:
typedef char * string;
string strings[100];
FILE* file = fopen(argv[1], "r");
char line[256];
while(fgets(line, sizeof(line), file))
{
string tmp = strtok(line, " ,'.-");
while(tmp != NULL)
{
strings[count]= tmp;
tmp = strtok(NULL, " ,.'-;");
count++;
}
}
int c2 = 0;
while(strings[c2] != NULL)
{
printf("%s, ", strings[c2]);
c2++;
}
return 0;
}
下面是从文件,我在阅读的文本:
There is a place where the sidewalk ends And before the street begins, And there the grass grows soft and white, And there the sun burns crimson bright, And there the moon-bird rests from his flight To cool in the peppermint wind.
'typedef char * string' - **从不。永远。** – 2013-03-29 23:20:35
恭喜。您有一个缓冲区溢出漏洞。 – 2013-03-29 23:21:41
所有指针指向'line',并被每个'fgets'覆盖。 –