代码之家  ›  专栏  ›  技术社区  ›  niXman

使用std::istream_迭代器从文件中读取行。谁?[复制品]

  •  1
  • niXman  · 技术社区  · 15 年前

    可能重复:
    How do I iterate over cin line by line in C++?

    我需要从文件中读取所有行:

    std::ifstream file("...");
    std::vector<std::string> svec(
       (std::istream_iterator<std::string>(file)),
       (std::istream_iterator<std::string>()),
    );
    

    但它被解读为文字。

    1 回复  |  直到 15 年前
        1
  •  2
  •   Thomas Matthews    15 年前

    我认为问题在于 std::string 将读取,直到找到空格字符,然后终止。

    你试过用吗 std::getline 在一个循环内?

    退房 C++ FAQ .