我的代码在.txt文件中停止读取

my code stopped reading in my .txt file

本文关键字:读取 文件 代码 txt 我的      更新时间:2023-10-16

我正试图让我的代码读取到一个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);
}

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

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

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