C ++: reading from a file with null characters

I need to read data from a file for assignment, unfortunately, instead of spaces separating the various fields, there are null characters. When I take integers from a file, they are extracted perfectly, but with strings I just get spaces and garbage from my uninitialized array of characters. Any ideas how to just extract the characters into my character array, ignoring the empty characters.

EDIT:

char fName[15],lName[15],pMethod[5],roomType[10],purpose[15];

int days, roomNum;

long guestID;

datafile>>guestID;
datafile.getline(fName,15,'\0');
datafile.getline(lName,15,'\0');

cout<<guestID<<endl;
cout<<fName<<endl;
cout<<lName<<endl;

is the code that I am using now, unfortunately, fName does not capture anything except zero and lName gets the value of the string fName. I thought about just getting the numbers as a string and converting them.

+3
source share
4 answers

getline \0 ( ) .

+4

std::getline , ('\n', ).

+4

http://www.cplusplus.com/reference/iostream/istream/read/ read the file into the buffer at a time, and then go from there.

0
source

Scroll bytes and ignore bytes of null character,

-1
source

Source: https://habr.com/ru/post/1772515/


All Articles