0

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.

Pietro
  • 10,628
  • 22
  • 80
  • 165
  • 3
    Please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude Apr 19 '21 at 18:28
  • 1
    `while(ifsFidMap.eof() == false) { std::getline(ifsFidMap, line); ... }` needs to be changed to `while(std::getline(ifsFidMap, line)) { ... }`. As for the debugger output - before the loop, the `line` string was completely empty, its internal buffer wasn't even allocated yet. After calling `getline()`, the string's buffer had been allocated, but it was still "empty" in that there was no data stored in it. – Remy Lebeau Apr 19 '21 at 18:32
  • @RemyLebeau - Thank you. A detail: is that representation of an empty string as `\\000\\000\\000\\000...` after calling `getline()` an issue of the debugger? – Pietro Apr 19 '21 at 21:56
  • @RemyLebeau - Sorry for my late reply. I just tried your suggestion, without any improvement. I updated my question. Thank you. – Pietro May 05 '21 at 17:07
  • @Pietro The code is fine (with the update I suggested earlier; I would also suggest changing `if(ifsFidMap.good() == false)` to `if(ifsFidMap.is_open() == false)`). The problem has to be with the debugger itself. – Remy Lebeau May 05 '21 at 17:11
  • @RemyLebeau - The problem is due to the compiler version... – Pietro May 05 '21 at 18:54

1 Answers1

0

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.

Pietro
  • 10,628
  • 22
  • 80
  • 165
  • Even so, what does this have to do with how the debugger displays the contents of the `string` after calling `getline()`? The code shown should work the same regardless of which C++ version is being used, as it is not using any features of C++11 or later. – Remy Lebeau May 05 '21 at 18:57
  • @RemyLebeau - I do not know, I am afraid I do not have the time to investigate deeper now. What I noticed is that compiling the code with successively more recent versions of GCC, I got to a point from where the builds were successful (GCC 5), and this happened when the default C++ used was C++11. – Pietro May 05 '21 at 22:13