简体   繁体   English

C++:ifstream::getline 问题

[英]C++: ifstream::getline problem

I am reading a file like this:我正在阅读这样的文件:

char string[256];

std::ifstream file( "file.txt" ); // open the level file.

if ( ! file ) // check if the file loaded fine.
{
    // error
}

while ( file.getline( string, 256, ' ' )  )
{
    // handle input
}

Just for testing purposes, my file is just one line, with a space at the end:仅出于测试目的,我的文件只有一行,末尾有一个空格:

12345 

My code first reads the 12345 successfully.我的代码首先成功读取了 12345。 But then instead of the loop ending, it reads another string, which seems to be a return/newline.但是,它不是循环结束,而是读取另一个字符串,这似乎是一个返回/换行符。

I have saved my file both in gedit and in nano .我已经在geditnano保存了我的文件。 And I have also outputted it with the Linux cat command, and there is no return on the end.而且我也用Linux cat命令输出过,最后没有返回。 So the file should be fine.所以文件应该没问题。

Why is my code reading a return/newline?为什么我的代码读取返回/换行符?

Thanks.谢谢。

First leets make sure your input file is good:首先 leet 确保您的输入文件是好的:

Run the following command and let us know the output:运行以下命令并让我们知道输出:

#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>>
#include <iomanip>
#include <algorithm>

int main()
{
    std::ifstream file("file.txt");
    std::cout << std::hex;

    std::copy(std::istreambuf_iterator<char>(file),
              std::istreambuf_iterator<char>(),

              std::ostream_iterator<int>(std::cout, " ")); 
}

Edit:编辑:

The output was 31 32 33 34 35 20 0A输出为 31 32 33 34 35 20 0A

Try running this code and see what the output is:尝试运行此代码,看看输出是什么:

#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>>
#include <iomanip>
#include <algorithm>

int main()
{
    std::ofstream file("file.txt");
    file << "12345 \n";
}

Dump the output of this file and compare it to the original.转储此文件的输出并将其与原始文件进行比较。
The problem is that different platforms have different line termination sequences.问题是不同的平台有不同的线路终止序列。 I just want to verify that '0x0A' is the line termination sequence for your platform.我只想验证 '0x0A' 是您平台的线路终止序列。 Note the line termination sequence is converted into a '\\n' when a file is read in text mode and when you output '\\n' to a file in text mode it is converted to the line termination sequence.请注意,当以文本模式读取文件时,行终止序列会转换为 '\\n',而在文本模式下将 '\\n' 输出到文件时,它会转换为行终止序列。

Edit 2编辑 2

So I have the file: file.txt所以我有文件:file.txt

> od -ta -tx1 file.txt
0000000    1   2   3   4   5  sp  nl                                    
           31  32  33  34  35  20  0a                                    
0000007

So the file contains 1 line terminated with 0x0A所以该文件包含 1 行以0x0A结尾

Using this program:使用这个程序:

#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>>
#include <iomanip>
#include <algorithm>

int main()
{
    std::ifstream   file("file.txt");

    std::string line;
    while(std::getline(file,line))
    {
        std::cout << "Line(" << line << ")\n";
    }
}

I get:我得到:

> g++ t.cpp
> ./a.out
Line(12345 )

it is working...它正在工作......

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

ifstream file("file.txt");

int main()
{
   string tmp="",st="";

   while (!file.eof())
    {
      file>>tmp;  
      if (tmp != "") st+=tmp;
      tmp="";  
    }
   cout<<st<<endl; 

   return 0;
}

input file.txt : 1 2 3 4 5输入文件.txt:1 2 3 4 5
answer : 12345答案:12345

Try this way:试试这个方法:

while ( !file.eof()  )
{
    file.getline( string, 256, ' ' );
        // handle input
}

It's old, but there does not seem to be proper resolution for this.它很旧,但似乎没有适当的解决方案。

I'm surprised that no one has noticed that he's using space delimiter.我很惊讶没有人注意到他正在使用空格分隔符。 Due to that the whole line won't be read, but only upto the first space.因此,不会读取整行,而只会读取到第一个空格。 Thus getline will still have more data to read before encountering EOF.因此 getline 在遇到 EOF 之前仍然有更多的数据要读取。

So the next getline will read newline and return the same as the delimiter is .所以下一个 getline 将读取换行符并返回与分隔符相同的 . If the getline call were like this:如果 getline 调用是这样的:

file.getline(string, 256) file.getline(字符串,256)

it'll not return newline and will finish in one step.它不会返回换行符,并将一步完成。

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

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