【发布时间】:2013-10-07 22:55:39
【问题描述】:
我的代码有问题。我正在尝试读取一些以前保存到文件中的命令,并将它们放在我的数组中以供以后使用。
这是我的相关代码:
if( (pastHist = fopen("history.txt", "r+")) == NULL)
{
pastHist = fopen("history.txt", "w+");
}
else
{
printf("%s", "INSIDE the else!");
pastHist = fopen("history.txt", "r+");
fscanf(pastHist, "%s", fstring);
while (fstring != NULL)
{
printf("%s %s", "the read in string is: ", fstring);
strcpy(cmndLine[cmndIndex], fstring);
strcpy(cmndLinecpy[cmndIndex], fstring);
cmndIndex++;
cmndNum++;
fscanf(pastHist, "%s", fstring);
}
}
现在代码可以正常写入文件。 (写作部分在别处举行)。如果我从我之前写过的文件中读取并且该文件说:
ls rmdir 天使 历史
然后我使用这个打印语句来仔细检查我正在阅读的内容......它会打印出来 "在 else 里面!读入的字符串是:ls 读入的字符串是:rmdir 读入的字符串是:angel 读入的字符串是:history 读入的字符串是:history 读入的字符串是:history
...它重复读到的最后一件事是历史一百万次。为什么会这样?我也尝试了 while 条件
while(getchar() != EOF)
但这给了我同样的东西。
请帮忙。 谢谢。
【问题讨论】: