0

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

6
  • Why are you passing random arguments to the ifstream constructor? Where did you get those from? What does the manual tell you about them? Commented Dec 3, 2016 at 3:21
  • Which random arguments are you referring to? I'm passing in the path and the flags to use. Commented Dec 3, 2016 at 3:23
  • @Igneous01 The std::ios::failbit | std::ios::badbit arguments. Commented Dec 3, 2016 at 3:24
  • 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. Commented Dec 3, 2016 at 3:30
  • 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. Commented Dec 3, 2016 at 3:38

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

I've updated my original post - still no joy. I forgot to mention that I tried without the flags first then appended them after (I saw a post on SO that mentioned adding these to constructor as they will throw exceptions).
0

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.

4 Comments

Interesting - exception certainly can take const char * as an argument for the message. What compiler are you using?
The linked page doesn't agree about const char*. My compiler is gcc 5.3.1 and clang 3.8.0.
I see now, vc++ has extensions to permit const char * as an argument for std::exception. msdn.microsoft.com/en-us/library/c4ts6d5a.aspx
Next idea: is it possible that the file t1.txt does not contain spaces (the symbol with code 32)? If so, then all the content of your file is put into the variable t and nothing is left for content.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.