【发布时间】:2013-12-08 16:44:36
【问题描述】:
我正试图让我的代码读入一个 txt 文件,它昨天还在工作,但现在当我通过“不调试启动”运行它时,它会打印出我提示它的消息,但就是这样,不管怎样我输入的内容会重新询问用户相同的问题,而不是打印 txt 文件中写入的内容。
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
void main ()
{
FILE *file_in;
char value;
file_in = NULL;
char unencrypted [1000];
while (file_in == NULL)
{
printf("Please enter the name of the file you want to open:\n");
scanf("%s", unencrypted);
file_in = fopen(unencrypted, "r");
}
printf("\n This file consists of the following message: \n");
while(!feof(file_in))
{
value = fgetc(file_in);
printf("%c", value);
}
fclose(file_in);
}
【问题讨论】:
-
你到底为什么同时使用
<fstream>和<cstdlib>?并在C++中使用 C 风格? -
您应该能够使用 errno 变量来获取更多信息。添加一行以在 fopen 返回 null 时打印 errno,这应该有助于您调试。
-
阅读这个“while(!feof(file))”总是错误的” - stackoverflow.com/questions/5431941/…
-
您应该通过将
main正确定义为int main来向操作系统返回错误代码。