2

I have the following problem- there is a file with information (I don't know the size of the file) like this:

\0\0\0\0\0\0123456789\0\0\0\0\0\0Name
\0\0\0\0\0\0111111111\0\0\0\0\0\0Name1
\0\0\0\0\0\0222222222\0\0\0\0\0\0Name2

and my goal is to read the numbers in the middle and the name at the end of the lines. I'm trying to read line by line using string like this:

std::ifstream fileStream(file, std::ios::binary);

if (fileStream.is_open()) 
{
    string line;
    while (getline(fileStream, line)) 
    {
        cout << line;
    }
}

but my output is : nothing

>
>
>

I guess that is because there are null characters in there and the string is terminated when the first null character is met, so that's why there is no output. I just don't know how to read the whole thing and then trim the null characters.

Any help would be appreciated.

vtodorova
  • 209
  • 2
  • 12
  • Are you guaranteed that there will always be 5 null terminators at the start and in the middle? – NathanOliver Aug 21 '18 at 14:08
  • yep, I think that's always the case – vtodorova Aug 21 '18 at 14:10
  • 6
    `getline` pretty much by definition traffics in **text**. You have a **binary** file. Use binary operations like the various flavors of `basic_istream::get()`. – Pete Becker Aug 21 '18 at 14:23
  • 1
    Cannot reproduce with GCC - I have written out a file with the format you use, and read and display it with the code you provide. –  Aug 21 '18 at 14:27
  • 2
    @Pete Where does it say that getline cannot read null characters? –  Aug 21 '18 at 14:29
  • @NeilButterworth -- it doesn't. Neither did I. – Pete Becker Aug 21 '18 at 14:29
  • 1
    @Pete So what do you mean by "a binary file"? –  Aug 21 '18 at 14:30
  • @NeilButterworth -- the code opens the file in binary mode. – Pete Becker Aug 21 '18 at 14:31
  • @Pete As I'm sure you know, that really only affects newline expansion - getline should still work OK (and does, in my tests). –  Aug 21 '18 at 14:34
  • @NeilButterworth -- sure, let's keep going down the rathole. The call to `getline` looks for a newline as the terminator. Turning off newline interpretation means that it can't be counted on to get line endings right. Encouraging people to ignore the difference between text and binary just because it happens to work sometimes is really bad advice. – Pete Becker Aug 21 '18 at 14:41
  • @Pete Newline expansion doesn't remove the newline, so getline will still see it. And on POSIX systems there is no difference between "text" and "binary" modes, and getline still seems to work on them. –  Aug 21 '18 at 14:46
  • @NeilButterworth -- you've got to be kidding. Newline expansion converts **whatever the file system uses to represent the end of a line** into the character `'\n'`. Turning off newline conversion means that you **cannot count on** getting a sensible match to the character `'\n'`. **Don't advise people to do that**. – Pete Becker Aug 21 '18 at 14:50
  • @NeilButterworth -- "it works on POSIX" is the twenty-year old UNIX-ist perspective that non-portable code is okay because it works on the systems you care about. I thought we had outgrown that perspective. – Pete Becker Aug 21 '18 at 14:51
  • @Pete Well, the tests I ran were actually on Windows, where it also worked –  Aug 21 '18 at 14:52
  • 1
    @NeilButterworth -- shrug. Iif you don't meet the preconditions of a standard library function that you call you have undefined behavior. Undefined behavior can often result in doing what you want to do, but it is, nonetheless, undefined. That's pretty much boilerplate on SO: don't write code whose behavior is undefined. – Pete Becker Aug 21 '18 at 14:54
  • @Pete Which precondidtions are you talking about? –  Aug 21 '18 at 14:55
  • If you redirect your program output to a file and then view the file in a hex editor, is there still no output? Are you sure your stream is open? I don't see your program orintingany diagnostic messages. – n. m. could be an AI Aug 21 '18 at 15:37
  • 1
    Cannot reproduce on CentOS 7 with gcc 5.3. `./a.out | xxd` shows the whole file, nul bytes included. – YSC Aug 21 '18 at 16:00

0 Answers0