Why ifstream getline is crashing

0

I wrote what I thought a quite basic function to read a file. But for a reason I dont know, the std::getline crashes my application, with this output :

The program '[14092] ASREngineApp.exe' has exited with code -1073740777 (0xc0000417)

My code

std::vector<std::string> readFile(const std::string& fileName)
{
    //Read file
    std::vector<std::string> fileLines;
    std::ifstream file;
    file.open("toto.txt", std::ios::in);
    if (!file)
    {
        //problem file NULL
        //return error
    }
    else if (!file.is_open())
    {
        //file not open
        //return error
    }
    else
    {
        //file opened
        std::string str;
        while (std::getline(file, str)) //this line crashes
        {
            fileLines.push_back(str);
        }
    }
    return fileLines;
}
  1. I triple checked and yes, toto.txt if a file that exists beside my application.
  2. I read this answer C++: ifstream::getline problem. This following snippet is also crasing: std::copy(std::istreambuf_iterator<char>(file),std::istreambuf_iterator<char>(),std::ostream_iterator<int>(std::cout, " "));
  3. I tried ofstream and I am able to create a file in the same folder where I am trying to open and read toto.txt
  4. I also double checked the permission of toto.txt, everything looks good.
  5. Compiling with vs2017
  6. Function like file.eof and file.is_open are not crashing, but function like file.tellg and file.seekg are crashing.

EDIT, more information, problem is the ifstream constructor

It looks like the problem is coming from std::ifstream constructor.

See this debug output from my little working application with only the readfile function called from a main: enter image description here

Then, the following is the debug output from the same function, in my real big application: enter image description here

So, why in this case, _Mychar is equal to -52'Ì'. This value should be '\0'

c++
visual-studio-2017
c++17
asked on Stack Overflow May 19, 2020 by peterphonic • edited Jun 20, 2020 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0