vendredi 2 décembre 2016

STD Filestream corrupted? (Internal File Buffer Null, unable to read input)

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, std::ios::in | std::ios::failbit | std::ios::badbit);
    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

I confirmed the path is correct

I confirmed that I am compiling under debug, with debug libraries

I have tried std::getline(file, t)

I have tried file.getline

I have tried using iterators

I have tried 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.

Aucun commentaire:

Enregistrer un commentaire