Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的代码停止读取我的.txt文件_C++_File_Input_Output - Fatal编程技术网

C++ 我的代码停止读取我的.txt文件

C++ 我的代码停止读取我的.txt文件,c++,file,input,output,C++,File,Input,Output,我试着让我的代码读入一个txt文件,它昨天还在工作,但现在当我运行它通过“启动而不调试”时,它会打印出我提示它的消息,但就是这样,无论我在其中键入什么,它都会重新问用户同样的问题,而不是打印txt文件中写的内容 #include <iostream> #include <fstream> #include <cmath> #include <cstdlib> void main () { FILE *file_in; char v

我试着让我的代码读入一个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);
}
#包括
#包括
#包括
#包括
空干管()
{
文件*FILE_in;
字符值;
文件_in=NULL;
未加密字符[1000];
while(file_in==NULL)
{
printf(“请输入要打开的文件名:\n”);
scanf(“%s”,未加密);
文件_in=fopen(未加密,“r”);
}
printf(“\n此文件由以下消息组成:\n”);
而(!feof(文件_in))
{
值=fgetc(文件中);
printf(“%c”,值);
}
fclose(文件输入);
}

如果它反复要求用户输入文件名,这意味着
fopen
返回NULL。您应该找出原因:

file_in = fopen(unencrypted, "r");
if (file_in == NULL)
    perror(unencrypted);

我不知道您在哪个平台上,但IDE通常会将发布版和调试版构建到不同的文件夹中。如果您将测试文本文件放在debug文件夹中,然后进行版本构建,则该文件将无法通过相对路径访问。

您究竟为什么同时使用
?在
C++
中使用C样式?您应该能够使用errno变量来获取更多信息。每当fopen返回null时,添加一行打印错误号,这将有助于您调试此错误。请阅读“while(!feof(file))”始终错误-您应该通过将
main
正确定义为
int main
来向操作系统返回错误代码。