使用流C ++阅读换行符 [英] Read newline using stream C++

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

问题描述

我还如何使用C ++ >>运算符读取换行符?

How do I also read a new line using C++ >> operator?

ifstream input("doc.txt".c_str());
vector<string> contents;
while (input >> word) {
    contents.push_back(word);
}

对于文件:

hello 
world
C++ is the best tool

应该返回

hello
\n
world
\n
C++
is
the
best
tool

P/S:从较大的角度来看,这是一个减少的问题.我解析文件的方式导致了此问题.

P/S: this is a reduced problem from a bigger one. The way I parse file lead to this problem.

推荐答案

您可以使用 push_back ,如

这篇关于使用流C ++阅读换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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