Search code examples
c++debuggingfstreamremote-debugging

Strange output from debugger on data from ifstream


I open a file in read only mode, I read a line, and I cannot understand the meaning of the line contents as shown by the debugger:

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

int main()
{
    std::string fidMapPath = "./file.txt";

    std::ifstream ifsFidMap(fidMapPath);

    if(ifsFidMap.good() == false) {
       std::cerr << "Error" << std::endl;
       exit(1);
    }
    
    std::string line;
    
    while(ifsFidMap.eof() == false)
    {
        std::getline(ifsFidMap, line);

        std::cout << "Line: " << line << std::endl;
    }
}

This is the contents of the text file:

; Document title
123;456
123;123
456;456
...

When running, nothing is printed from the line variable; with the debugger, its contents is equal to "" (empty) before getline(), and \\000\\000\\000\\000... after, repeated up to a length of 2411 characters.

What is the meaning of this behavior?

These are my platform details:

  • Operating system: Windows 10, building remotely on Linux (Red Hat, kernel 2.6.32-431.5.1.el6.x86_64) through NetBeans 8.2.
  • Compiler: GCC 4.8.2 (C++11)
  • Debugger: GDB Red Hat Enterprise Linux 7.6.1-47.el6

P.S.: I tried, as suggested, to move the getline in the while argument:

while(std::getline(ifsFidMap, line))

but I still have the same issue.


Solution

  • With the version of GCC I used, 4.8.2, we must explicitly specify if what being compiled is C++11 code with the -std=c++11 compiler option. Otherwise it defaults to C++98.

    This applies to GCC versions up to 5.x.