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

查看:91
本文介绍了C ++:ifstream :: getline问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取一个这样的文件:

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 

我的代码首先成功读取了12345.但是,它不是读取循环结尾,而是读取另一个字符串,该字符串似乎是return/newline.

My code first reads the 12345 successfully. But then instead of the loop ending, it reads another string, which seems to be a return/newline.

我已将文件保存在 gedit nano 中.而且我还使用Linux cat 命令将其输出,最终没有任何回报.因此文件应该没问题.

I have saved my file both in gedit and in nano. And I have also outputted it with the Linux cat command, and there is no return on the end. So the file should be fine.

为什么我的代码读取退货/换行符?

Why is my code reading a return/newline?

谢谢.

推荐答案

首先,请确保您的输入文件正确:

First leets make sure your input file is good:

运行以下命令,让我们知道输出:

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, " ")); 
}

输出为31 32 33 34 35 20 0A

The output was 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";
}

转储此文件的输出,并将其与原始文件进行比较.
问题在于不同的平台具有不同的线路终端顺序.我只想确认"0x0A"是您平台的线路终止顺序.请注意,在文本模式下读取文件时,行终止顺序将转换为"\ n",而在文本模式下将"\ 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. 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.

所以我有文件:file.txt

So I have the file: file.txt

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

因此文件包含1行,以 0x0A

So the file contains 1 line terminated with 0x0A

使用此程序:

#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";
    }
}

我得到:

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

这篇关于C ++:ifstream :: getline问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆