Read newline using stream C++

前端 未结 4 2049
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 18:08

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

ifstream input(\"doc.txt\".c_str());
vector contents;
while (input >> word) {
    conten         


        
4条回答
  •  醉话见心
    2021-01-26 18:21

    You can use std::getline, and push_back the "\n" yourself, as mentioned by jaggedSpire:

    std::ifstream input("doc.txt");
    std::vector contents;
    
    for (std::string line; std::getline(input, line);) {
        std::istringstream str(line);
    
        for (std::string word; str >> word;) {
            contents.push_back(word);
        }
    
        contents.push_back("\n");
    }
    

提交回复
热议问题