用C ++读取管道输入 [英] Reading piped input with C++

查看:158
本文介绍了用C ++读取管道输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

#include <iostream>
using namespace std;

int main(int argc, char **argv) {
    string lineInput = " ";
    while(lineInput.length()>0) {
        cin >> lineInput;
        cout << lineInput;
    }
    return 0;
}

使用以下命令:
echo Hello| test.exe

这些结果是一个无限循环打印Hello。如何让它读取并打印一个单独的Hello?

Thes result is an infinate loop printing "Hello". How can I make it read and print a single "Hello"?

推荐答案

string lineInput;
while (cin >> lineInput) {
  cout << lineInput;
}

如果你真的想要全线,请使用:

If you really want full lines, use:

string lineInput;
while (getline(cin,lineInput)) {
  cout << lineInput;
}

这篇关于用C ++读取管道输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆