Getline to String复制换行符 [英] Getline to String Copies newline as well

查看:259
本文介绍了Getline to String复制换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在逐行读取文件,并将每行添加到字符串。然而,字符串长度增加1每一行,我相信是由于换行符。如何删除它被复制。

I am reading a file line by line and adding each line to a string. However the string length increases by 1 for every line which I believe is due to newline character. How can I remove it from being copied.

这里是我的代码尝试做同样的。

Here is my code attempt to do the same.

if (inputFile.is_open())
{
    {
        string currentLine;
        while (!inputFile.eof())
            while( getline( inputFile, currentLine ) )
            {
                string s1=currentLine;
                cout<<s1.length();
            }

[更新说明]我使用notepad ++来确定我的长度逐行选择。所以他们显示一些123,450,500,120,我的程序显示124,451,501,120。除了最后一行,所有的line.length()显示一个增加的值。

[Updated Description] i have used notepad++ to determine the length of what i am selecting line by line. So they are showing some 123, 450, 500, 120 for which my program shows 124,451,501,120. Except for the last line, all line.length() shows an increased by 1 value.

推荐答案

$ c> inputFile 有Windows样式的换行符(CRLF),但您的程序正在拆分类似Unix的换行符(LF)上的输入,因为 std :: getline() ,默认打开 \\\
,留下CR( \r )。

It looks like inputFile has Windows-style line-breaks (CRLF) but your program is splitting the input on Unix-like line-breaks (LF), because std::getline(), breaks on \n by default, leaving the CR (\r) at the end of your string.

您需要修剪无关的 \r 。这里有一种方法,以及一个小测试:

You'll need to trim the extraneous \rs. Here is one way to do it, along with a small test:

#include <iostream>
#include <sstream>
#include <iomanip>

void remove_carriage_return(std::string& line)
{
    if (*line.rbegin() == '\r')
    {
        line.erase(line.length() - 1);
    }
}

void find_line_lengths(std::istream& inputFile, std::ostream& output)
{
    std::string currentLine;
    while (std::getline(inputFile, currentLine))
    {
        remove_carriage_return(currentLine);
        output
            << "The current line is "
            << currentLine.length()
            << " characters long and ends with '0x"
            << std::setw(2) << std::setfill('0') << std::hex
            << static_cast<int>(*currentLine.rbegin())
            << "'"
            << std::endl;
    }
}

int main()
{
    std::istringstream test_data(
        "\n"
        "1\n"
        "12\n"
        "123\n"
        "\r\n"
        "1\r\n"
        "12\r\n"
        "123\r\n"
        );

    find_line_lengths(test_data, std::cout);
}

输出:

The current line is 0 characters long and ends with '0x00'
The current line is 1 characters long and ends with '0x31'
The current line is 2 characters long and ends with '0x32'
The current line is 3 characters long and ends with '0x33'
The current line is 0 characters long and ends with '0x00'
The current line is 1 characters long and ends with '0x31'
The current line is 2 characters long and ends with '0x32'
The current line is 3 characters long and ends with '0x33'

注意事项:


  • t需要测试EOF。 std :: getline() 将返回流,当它可以从 inputFile 中不再读取时,将转换为 false / li>
  • 您不需要复制字符串来确定其长度。

  • You don't need to test for EOF. std::getline() will return the stream, which will cast to false when it can read no more from inputFile.
  • You don't need to copy a string to determine its length.

这篇关于Getline to String复制换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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