简体   繁体   中英

C++: reading from a file with null characters

I have to read data from a file for an assignment unfortunately instead of spaces separating the various fields there are null characters. When taking integers from the file they are extracted fine however with the strings i just get a blanks space and garbage from my uninitialized character array. Any ideas as how to just extract the characters into my character array ignoring the null 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 I'm using now unfortunately fName isnt grabbing anything other than null again and lName is getting fName's string value. Was thinking about just getting the numbers as string and converting them.

std::getline有一个可选参数,它是定界符(默认为'\\n' )。

使用getline并传递\\0 (空字符)作为分隔符。

http://www.cplusplus.com/reference/iostream/istream/read/一次性读取文件以进行缓冲,然后从那里继续。

循环遍历字节,并忽略空字符字节,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM