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

时间:2013-12-08 16:44:37

标签: c++ file input output

我试图让我的代码在txt文件中读取,它在昨天工作但是现在当我运行它时#34;开始没有调试"它会打印出我提示它的消息,但不管我输入的是什么,重新询问用户相同的问题而不是打印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);
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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