How to read entire stream into a std::string?

后端 未结 7 1005
灰色年华
灰色年华 2020-11-29 19:15

I\'m trying to read an entire stream (multiple lines) into a string.

I\'m using this code, and it works, but it\'s offending my sense of style... Surely there\'s an

7条回答
  •  孤街浪徒
    2020-11-29 19:30

    What about to use getline with delimiter? The next code helps me to read whole std::cin into string on ubuntu with g++-10.

    #include 
    #include 
    
    using namespace std;
    
    int main() {
        string s;
    
        getline(cin, s, {}); //the whole stream into variable s
    
        return 0;
    }
    

提交回复
热议问题