0
votes

This simple program, cannot read data using ifstream. The read is always an empty string.

#include <fstream>
#include <string>
#include <iostream>

std::string getFileString(const std::string& path)
{
    std::ifstream file(path);
    if (!file) 
       throw std::exception("Cannot Open File");
    if (!file.is_open() && !file.good())
       throw std::exception("Cannot Open File");
    std::string t;
    file >> t;
    std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

    return content;
}

int main()
{
    std::string t = getFileString("C:\\Users\\user\\Documents\\folder\\t1.txt");
    return 0;
}

I have tried debugging this for a few hours, and there's no way to fix it. I've tried in new projects to do this exact same thing, and the ifstream doesn't read anything.

I confirmed:

  • there is data in the file

  • the path is correct

  • I am compiling under debug, with debug libraries

I have tried:

  • std::getline(file, t)

  • file.getline

  • using iterators

  • using the >> operator

No exceptions are thrown (even with ios::failbit and ios::badbit set)

file is good

file.is_open() is true and file.good() is true

When I examine the file stream object in debugger (after constructor call) it appears to be corrupted (the entire buffer is null, bunch of nulls in the pointers, the only valid data is the reference to the file itself).

ifstream in debug

I have compiled for both win32 and x64 and I get the same result.

I repaired my installation of C++ distributable for VS 2012, and it's the same issue.

I have no idea what to do at this point. I have never seen an issue like this before. I'm beginning to wonder if other std objects are also corrupted like this one. Or maybe it's the C file stream that is corrupted in some shape.

EDIT*** I have removed the arguments, but whatever data is being read is not what is from my file.

enter image description here

3
Why are you passing random arguments to the ifstream constructor? Where did you get those from? What does the manual tell you about them? - Kerrek SB
Which random arguments are you referring to? I'm passing in the path and the flags to use. - Igneous01
@Igneous01 The std::ios::failbit | std::ios::badbit arguments. - user1593881
I updated my post - sorry I should have shown the original code without the flags. I added those afterwards after reading on SO that those control whether exceptions get thrown from ifstream. - Igneous01
Use || instead of && in !file.is_open() && !file.good(). With &&, both condition need to be true (i.e., file is not open AND file is not good) for the exception to be thrown. - 1201ProgramAlarm

3 Answers

2
votes
std::ifstream file(path, std::ios::in | std::ios::failbit | std::ios::badbit);

You created a file object that's already flagged as being in a failed state, immediately after it's constructed.

Now, if that's not what you wanted, kindly get rid of the failbit and the badbit. In fact, get rid of that entire parameter completely, since the 2nd parameter to ifstream's constructor defaults to ios::in anyway.

0
votes

I can't compile your sample program. And cppreference says that exception can't be constructed with char* argument. Use instead, for example, runtime_error.

My suggestion is that you test some wrong program. Maybe your IDE fails to compile the source code, but somehow misses it and debugs some old version of the binary code.

0
votes

I had the same bug, it’s normal for fstream to be all Null after construction, lol I checked many times in many new projects. That’s why both good() and is_open() was true. The problem is how data in the file is read, the file content is not in a format that you are reading it I think, using stream iterators, it would cause a read of the type you passed to it on construction with the stream, well in my case It is causing a read past the end of the stream because the data format I thought was saved to the file wasn’t actually that. In yours I think the file is a binary file, try using unsigned char rather than just char. Hopefully that fixes it