C++ ifstream 不读\n?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15706776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:40:48  来源:igfitidea点击:

C++ ifstream not reading \n?

c++textprojectnewlineifstream

提问by PulsePanda

I'm doing a project for school, and i have to read a file, and do stuff with it. that isnt important though. i'm supposed to do the operations per line, and the way i have it right now, my ifstream object isnt detecting the '\n'character for each line, which is required for this project. here's how i have it right now:

我正在为学校做一个项目,我必须阅读一个文件,并用它做一些事情。不过这并不重要。我应该按行进行操作,按照我现在的方式,我的 ifstream 对象没有检测到'\n'每个行的字符,这是该项目所需的。这是我现在的方式:

ifstream instream;
instream >> value;
while(value != '\n')
    //do code and such

but when i have it run the loop, all i'm getting is a single line of everything in the program. while, it is doing exactly what it is supposed to in the loop, i NEED the \n to be recognized. here's my .txt document:

但是当我让它运行循环时,我得到的只是程序中所有内容的一行。同时,它正在执行循环中应该做的事情,我需要识别 \n。这是我的 .txt 文档:

LXXXVII
cCxiX
MCCCLIV
CXXXLL
MMDCLXXIII
DXLCC
MCdLxxvI
XZ
X
IV

exactly like that. i cannot change it. any assistance as to what i can do would be greatly appreciated! thanks a ton!

正是这样。我不能改变它。任何关于我能做什么的帮助将不胜感激!万分感谢!

EDIT:also, how would i be able to detect when i'm at the end of the file? thanks :D

编辑:另外,我如何能够检测到我何时处于文件末尾?感谢:D

回答by Jonathan Wakely

The >>operator does a "formatted input operation" which means (among other things) it skips whitespace.

所述>>操作者做了“格式的输入操作”它跳过空白其中装置(除其他外)。

To read raw characters one by one without skipping whitespace you need to use an "unformatted input operation" such as istream::get(). Assuming valueis of type char, you can read each char with instream.get(value)

要逐个读取原始字符而不跳过空格,您需要使用“无格式输入操作”,例如istream::get(). 假设value类型为char,您可以读取每个字符instream.get(value)

When you reach EOF the read will fail, so you can read every character in a loop such as:

当您到达 EOF 时,读取将失败,因此您可以读取循环中的每个字符,例如:

while (instream.get(value))
  // process value

However, to read line-by-line you could read into a std::stringand use std::getline

但是,要逐行阅读,您可以读入 astd::string并使用std::getline

std::string line;
while (getline(instream, line))
  // ...

This is an unformatted input operation which reads everything up to a \ninto the string, then discards the \ncharacter (so you'd need to manually append a \nafter each erad line to reconstruct the original input)

这是一个未格式化的输入操作,它将 a 之前的所有内容读\n入字符串,然后丢弃该\n字符(因此您需要\n在每个 erad 行后手动附加 a以重建原始输入)

回答by taocp

You can read your file as follows:

您可以按如下方式阅读您的文件:

 ifstream instream("file.txt);
 string line;
 while (instream >> line)
 {
    cout << line;
    if (instream.peek() == '\n') //detect "\n"
    {
       cout <<endl;
    }
 }
 instream.close();

This way you can track where the line in file ends and detect end of file.

通过这种方式,您可以跟踪文件中的行结束位置并检测文件结束位置。

回答by Afterbunny

I guess you are looking for this: instream.unsetf(std::ios_base::skipws)

我猜你正在寻找这个: instream.unsetf(std::ios_base::skipws)