-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'覆蓋。 –