使用 std::istream_iterator 从文件中读取行。 WHO?
可能的重复:
如何逐行迭代 cin C++?
我需要读取文件中的所有行:
std::ifstream file("...");
std::vector<std::string> svec(
(std::istream_iterator<std::string>(file)),
(std::istream_iterator<std::string>()),
);
但它被读取为单词。
Possible Duplicate:
How do I iterate over cin line by line in C++?
I need to read all lines from a file:
std::ifstream file("...");
std::vector<std::string> svec(
(std::istream_iterator<std::string>(file)),
(std::istream_iterator<std::string>()),
);
but it is read as words.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于
std::string
的输入方法将读取直到找到空格字符,然后终止。您是否尝试过在循环内使用
std::getline
?查看C++ 常见问题解答。
I believe the issue is that the input methods for
std::string
will read until a space character is found, then terminate.Have you tried using
std::getline
inside a loop?Check out the C++ FAQ.