0

Why does fin.fail() return true in the following code?

void loadName(){
    int pointer; //holds size of file
    fin.open("C:\\Users\\iAMspecial\\Desktop\\Names.txt", ios::app);
    if (fin.fail()) {
        cerr << "could not open intput file names.txt" << endl;
        system("pause"); 
        exit(1);
    }
    pointer++;
    getline(fin,Names[pointer]);
    for(int ndx = 0; !fin.eof(); ndx++){
        pointer++;  
        getline(fin,Names[pointer]);
    }
    fin.close();
    counter = pointer;
}

I've been struggling with std::ifstream in this function. I've scouted the other questions and even with all the advice, I can’t seem to get the function working.

A lot of the issues also seem to stem from Visual Studio, however I'm using a different IDE. Apologies in advance if I missed something really stupid.

I've made doubly sure of the file path, it is 100% correct. I'm truly stumped.

Picture of output:

Picture of output

The program is quite long, however if any other parts of it are relevant to the issues I'm having I'm happy to post it.

(Also note that the file path is temporary, I'm merely trying to have the function work, at that point I will have it work with different file paths).

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • Define "having problems". Also provide a [mcve]. – Passer By Nov 21 '17 at 06:16
  • Sorry about there being no real question, I edited the post. Why is fin.fail() coming back as true? The snippet of code in question without fail reproduces the issue, it never seems to find "Names.txt" despite me providing an exact path. – iAMspecial Nov 21 '17 at 06:17
  • The file is in system drive i.e. `C:` drive. Try with a file in another drive. Or run program with `admin` access. Because access level may create problem in accessing the file. – cse Nov 21 '17 at 06:24
  • Please do not post text as image. Paste the text directly into your question. – Melebius Nov 21 '17 at 06:28
  • Never use `fail` before any reading/writing operation. Its purpose is not to test for open status but for reading/writing status. – Jean-Baptiste Yunès Nov 21 '17 at 06:40
  • Can you open the file for read only? – Killzone Kid Nov 21 '17 at 06:53
  • Neither the input file or the executable itself is set to read only. – iAMspecial Nov 21 '17 at 07:03
  • Unrelated: `for(int ndx = 0; !fin.eof(); ndx++){` is a variant of `while (! eof)` and that [has a well-known history of causing problems](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – user4581301 Nov 21 '17 at 07:39

2 Answers2

3

Use fin.is_open() instead. fin.fail() is not for checking stream open errors.

if (!fin.is_open()) {
    cerr << "Error: " << strerror(errno);
}

Also, the correct way to read file line-by-line is

std::string line;
while (getline(fin, line)) {
    // Do whatever with line
}
Shreevardhan
  • 10,417
  • 3
  • 32
  • 44
  • I made the suggested change with my if loop. "if (!fin.is_open())" However it's still producing the same output. I also tried moving my ifstream declaration to the function to no avail. – iAMspecial Nov 21 '17 at 06:28
  • @iAMspecial Do you see a helpful error message? Do you have permissions to write the file through the code's executable? – Shreevardhan Nov 21 '17 at 06:31
  • I ran the program through its executable in administrator mode, it didn't produce any error messages. I did however remove the if loop all together, the program merely froze, but not crashed once I ran the load function. – iAMspecial Nov 21 '17 at 06:34
  • By no error message I mean the output was "Error: No error" – iAMspecial Nov 21 '17 at 06:52
0

Every system call that fails update the errno value.

You can have more information about what went wrong if you will print it:

cerr << "Error: " << strerror(errno);
OriBS
  • 614
  • 4
  • 9