C++:为什么 cout 使用 ifstream 从文件中读取的字符串打印回车? [英] C++: Why cout is printing carriage return with the strings read from a file using ifstream?

查看:55
本文介绍了C++:为什么 cout 使用 ifstream 从文件中读取的字符串打印回车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取一个文件 capitals,其内容如下:

I'm trying to read a file capitals with contents as follows:

Tokyo   
33200000
New York    
17800000
Sao Paulo   
17700000
Seoul
17500000
Mexico City
17400000

我用来打印文件内容的代码片段是:

The code snippet I'm using to print the file contents is:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    std::ifstream ifs("capitals");
    std::string s, s2;

    while (getline(ifs, s)) {
        getline(ifs, s2);
        cout << s  << ": "
             << s2 << endl;
    } 

    return 0;
}

但是,我得到的输出如下:

But, the output I'm getting is as follows:

code_master5@Brahmaastra:~$ g++ test.cpp -std=c++17
code_master5@Brahmaastra:~$ ./a.out 
: 33200000
: 17800000  
: 17700000  
: 17500000
: 17400000y

预期的输出应该是这样的:

The expected output should be of form:

Tokyo: 33200000

原始输出的最后一行表明 cout 在打印城市名称后打印 回车.为什么会发生这种情况?

The last line of the original output suggests that cout is printing carriage return after printing the city name. Why is this happening?

编辑 1: 正如 @Qubit 所建议的,我使用 pop_back() 删除了最后一个字符.更改后的代码是:

EDIT 1: As suggested by @Qubit I used pop_back() to remove last character. The changed code is:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    std::ifstream ifs("capitals");
    std::string s, s2;

    while (getline(ifs, s)) {
        s.pop_back();
        getline(ifs, s2);
        s2.pop_back();
        cout << s  << ": "
             << s2 << endl;
    } 

    return 0;
}

现在的输出是:

Tokyo   : 33200000
New York    : 17800000
Sao Paulo   : 17700000
Seoul: 17500000
Mexico City: 17400000

这些额外的空格是什么?

What are these extra spaces?

EDIT 2 现在问题解决了.正如 @lubgr 所建议的,发生这种情况是因为文件以 windows 样式存储,每行末尾都有 \r.所以,我安装了 dos2unix 并运行以下命令:

EDIT 2 The problem is solved now. As suggested by @lubgr, this is happening because the file is stored in windows-style with \r at end of each line. So, I installed dos2unix and ran the following command:

code_master5@Brahmaastra:~$ dos2unix capitals 
dos2unix: converting file capitals to Unix format... 

然后,正如@codekaizer 进一步解释的那样,我保留了那些 pop_back() 调用以删除尾随的 '\t' 字符以获得预期的输出.

Then, as further explained by @codekaizer, I kept those pop_back() calls to remove the trailing '\t' characters to get the expected output.

推荐答案

这是由你的 capitals 文件引起的,它有一个以回车符结尾的行(windows 样式),但是当你打印它时在非 Windows 终端中,这些回车没有得到正确处理.你有两个选择,要么运行

This is caused by your capitals file, it has line ending with a carriage return (windows style), but when you print it in a non-Windows terminal, these carriage returns aren't properly handled. You have two options, either run

dos2unix captials

这将从行尾中删除所有回车字符,或者按照@Qubit 在注释中的建议,您可以删除 while 循环中的最后一个字符:

which will remove all carriage return characters from the line endings, or as suggested by @Qubit in the comments, you get rid of the last character inside the while loop:

while (getline(ifs, s)) {
    getline(ifs, s2);
    s.pop_back();
    s2.pop_back();

    /* ... */
}

这篇关于C++:为什么 cout 使用 ifstream 从文件中读取的字符串打印回车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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