Getline and 16h (26d) character

2019-07-30 03:17发布

in VC++ environment Im using (string) getline function to read separate lines in opened file. Problem is that getline takes character 1Ah as end of file and if it is present on the line, whole reading ends prematurely. Is there any solution for this?

Code snippet:

fstream LogFile (Source,fstream::in);
string Line

while (getline(LogFile,Line))
{  ....  }

File contents:

line1text1asdf
line2text2asd //EOF for getline here
line3asdas // this line will never be read by getline

Thank you for any info.

Kra

标签: c++ getline
3条回答
Explosion°爆炸
2楼-- · 2019-07-30 03:57

Replace getline with a hand-rolled function that reads in character by character until end of line or eof, as defined by you.

查看更多
闹够了就滚
3楼-- · 2019-07-30 03:57

I usually prefer to open the file as binary, read the data with the function below, and parse for '\n' and '\r' to detect end of lines.

UINT xread(HFILE hfile, void *buf, UINT size)
{
   UINT ret;


   #if defined(_WIN32)

   ret = _read(hfile, buf, size);

   #elif defined(_LINUX) || defined(__APPLE__)

   ret = read(hfile, buf, size);

   #endif


   return(ret);
}
查看更多
一纸荒年 Trace。
4楼-- · 2019-07-30 04:00

Yes, Ctrl+Z was the EOF file character for text files in ancient operating systems. It is a control character that really shouldn't be present in a text file, you can't meaningful translate it. Openmode::binary is about all you can do if that's required.

查看更多
登录 后发表回答