我的代码在.txt文件中停止读取
my code stopped reading in my .txt file
我正试图让我的代码读取到一个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通常会将发布和调试构建构建到不同的文件夹中。如果您将测试文本文件放在调试文件夹中,然后进行发布构建,则无法通过相对路径访问该文件。
相关文章:
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 读取文件的最后一行并输入到链接列表时出错
- 为什么在读取文件大小时文件IO速度会发生变化
- 读取文件时运行时的未知行为
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- C++ 读取文件读取文件不正确
- 读取文件并将其存储在unordered_map中时出现问题
- 读取文件时无法使用 OpenMP 获得加速
- 使用istringstearm和get行缓慢读取文件
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 读取文件时引发异常
- 从标准输入读取文件后读取用户输入
- 在读取文件后重置句柄
- 如何在C++编译时读取文件?
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- 读取文件在第二次调用时返回INVALID_HANDLE
- 通过指针读取文件
- 逐行读取文件,并将数据插入变量和数组中