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'

Community
  • 1
  • 1
peterphonic
  • 951
  • 1
  • 19
  • 38
  • 1
    1. Your error is in some other place. 2. You don't need `!file.is_open`. – zdf May 19 '20 at 21:39
  • 2
    Your function never uses its `fileName` argument. So why does it take one in the first place? – Jesper Juhl May 19 '20 at 21:40
  • I can't reproduce https://wandbox.org/permlink/qFBbXqa1IamP11Ln – Thomas Sablik May 19 '20 at 21:41
  • 1
    Maybe there is some type of undefined behavior in your program before you open this file that causes the heap to be corrupt or some other type of corruotion. – drescherjm May 19 '20 at 21:44
  • Related: [https://stackoverflow.com/questions/55138498/how-can-i-debug-the-cause-of-a-0xc0000417-exit-code](https://stackoverflow.com/questions/55138498/how-can-i-debug-the-cause-of-a-0xc0000417-exit-code) – drescherjm May 19 '20 at 21:49
  • "yes, toto.txt if a file that exists beside my application" - that it exists in the same location as your executable is not what is important. Your program tries to open the file from its current working directory. That is *not necessarily* the same as the directory your application is in, depending on how it was started and whether or not the program programmatically changed its CWD at run time. – Jesper Juhl May 19 '20 at 21:51
  • @JesperJuhl I also tried with an absolute path, I have the same crash. Really, this doesn't make sense.... – peterphonic May 19 '20 at 21:55
  • I don't believe the code displayed is the problem. My advice is to create a new small example program and see if it still exhibits this failure. If your small example program still crashes then its time to try reinstalling Visual Studio. – drescherjm May 19 '20 at 21:58
  • @drescherjm Did a small project with just the snippet above and it worked. So, you are right, the problem is coming from somewhere else. But how can I debug this strange problem? – peterphonic May 19 '20 at 22:09
  • First step is to turn the warning level up and check for compiler warnings. A second step could be a bit of calling the function at different places in your program seeing where it is working and where it causes a crash. – drescherjm May 19 '20 at 22:16

0 Answers0