user3080241
user3080241

Reputation: 1

my code stopped reading in my .txt file

I'm trying to get my code to read in a txt file, it was working yesterday but now when I run it through "start without debugging" it prints out the message I prompted it to but that's it, and no matter what I type in it re-asks the user the same question instead of printing what was written in the txt file.

#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);
}

Upvotes: 0

Views: 65

Answers (2)

Stephan van den Heuvel
Stephan van den Heuvel

Reputation: 2138

I don't know what platform you are on, but IDEs typically build release and debug builds to different folders. If you put your test text file in the debug folder, and then do a release build, the file will be unreachable with a relative path.

Upvotes: 0

Tim Pierce
Tim Pierce

Reputation: 5664

If it repeatedly asks the user to enter a file name, that means that fopen is returning NULL. You should find out why:

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

Upvotes: 1

Related Questions