使用 std::istream_iterator 从文件中读取行。 WHO?

发布于 2024-09-02 12:04:19 字数 420 浏览 1 评论 0原文

可能的重复:
如何逐行迭代 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉絮湮 2024-09-09 12:04:19

我认为问题在于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文