Reading piped input with C++

后端 未结 2 738
萌比男神i
萌比男神i 2020-12-10 02:56

I am using the following code:

#include 
using namespace std;

int main(int argc, char **argv) {
    string lineInput = \" \";
    while(line         


        
相关标签:
2条回答
  • 2020-12-10 03:39

    When cin fails to extract, it doesn't change the target variable. So whatever string your program last read successfully is stuck in lineInput.

    You need to check cin.fail(), and Erik has shown the preferred way to do that.

    0 讨论(0)
  • 2020-12-10 03:50
    string lineInput;
    while (cin >> lineInput) {
      cout << lineInput;
    }
    

    If you really want full lines, use:

    string lineInput;
    while (getline(cin,lineInput)) {
      cout << lineInput;
    }
    
    0 讨论(0)
提交回复
热议问题