简体   繁体   English

C ++如何为ifstream成员正确使用getline

[英]C++ How do I properly use getline for ifstream members

Ok so I have a problem with getline. 好的,我对getline有问题。

I have a file that contains a couple strings. 我有一个包含几个字符串的文件。 I created it by myself and I have each string on a seperate line. 我自己创建了它,每个字符串都放在单独的行上。

Ex. textfile.txt
    Line 1
    Line 2
    Line 3
    Line 4

//Little snip of code
    ifstream inFile("textfile.txt");
    getline(inFile, string1);

When I debug the program and ask it to print out string1 it shows that "Line 1\\r" is saved into string1. 当我调试程序并要求它打印出string1时,它表明“ Line 1 \\ r”已保存到string1中。 I understand that it's from me actually hitting enter when I created the file. 我知道这是我创建文件时实际上按Enter的原因。 This problem causes my program to have a segmentation fault. 此问题导致我的程序出现分段错误。 I know my code works because if I use ofstream to write the file first and then i read it in, it works. 我知道我的代码有效,因为如果我先使用ofstream写入文件,然后再读入它,则可以正常工作。

So for my quesiton, is their anyway to use the getline function without it picking up the escape sequence \\r? 因此,对于我的问题,他们是否仍要使用getline函数而不选择转义序列\\ r? If i am not clear just let me know. 如果我不清楚,请告诉我。

The fact that you have the '\\r' character is unlikely to cause a seg fault by itself. 您具有'\\ r'字符的事实本身不太可能导致段错误。

Other mysteries: 其他奥秘:
If you open a file for writing in binary the output is exactly what you output. 如果打开文件以二进制形式写入,则输出将与您输出的完全相同。
If you open a file for writing in text (the default) the output is the same except for '\\n', which is transformed into a line termination sequence (more on that below). 如果打开一个文件来写文本(默认设置),则输出相同,除了“ \\ n”,后者被转换为行终止顺序(更多信息见下文)。

If you open a file for reading in binary the input is exactly what is in the file. 如果打开文件以二进制形式读取,则输入内容恰好是文件中的内容。
If you open a file for reading in text (the default) the input is the same as the file except for the line termination sequence, which is transformed into the character '\\n'. 如果打开文件以读取文本(默认设置),则输入与文件相同,只是行终止顺序被转换为字符“ \\ n”。

In normal situations this is fine. 在正常情况下,这很好。 But every platform seems to have its own definition of the line termination sequence. 但是每个平台似乎对线路终止顺序都有自己的定义。 Thus if you write a text file on a MAC then read it on a PC it may not work as expected. 因此,如果您在MAC上写入文本文件,然后在PC上读取文本文件,则可能无法正常工作。

So unless you are doing one of the following everything should work. 因此,除非您执行以下操作之一,否则一切都应该正常工作。

  • Writing in Binary/Reading in Text 二进制写作/文本阅读
  • Writing in Text/Reading in Binary 文字写作/二进制阅读
  • Writing the file and OS1/Reading the file on OS2 写入文件和OS1 /在OS2上读取文件
    • Where OS1/OS2 do not have the same line termination sequence. 其中OS1 / OS2没有相同的线路终止顺序。

Note 1: If you write the file in Binary mode. 注1:如果以二进制模式写入文件。 You should not be using getline() which assumes text mode (it is expecting the line termination sequence to split lines). 您不应该使用假定文本模式的getline()(它期望行终止序列分割行)。 Sub Note: For the pedantic(s) out there the getline() may work on binary but this is probably relying on implementation details and you should not consider it portable. 子注意:对于那里的学究型车,getline()可能适用于二进制程序,但这可能取决于实现细节,您不应该将其视为可移植的。

Note 2: The above assumes you have not installed a specialized local that implements a facet that transforms stream data on the fly. 注意2:以上假设您尚未安装实现可即时转换流数据的构面的专用本地程序。

If your standard library is correctly implemented, the terminating character should be removed from the stream, but not appended to the string. 如果正确实现了标准库,则应从流中删除终止字符,但不要将其附加到字符串中。 Even if it was appended to the string, that shouldn't cause a segfault -- I think your problem lies elsewhere. 即使将其附加到字符串中,也不应引起段错误-我认为您的问题出在其他地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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