C++ 如何在C+中逐行读取管道命令+;?

C++ 如何在C+中逐行读取管道命令+;?,c++,shell,stdin,C++,Shell,Stdin,我有一个名为example.cpp的cpp程序。在这个程序中,我想逐行读取标准数据,并将每一行保存在一个字符串中。但是,它不是来自关键字或文件。输入来自命令行: CC example.cpp -o ALO last -n 10 | ./ALO 我尝试了以下几种方法,但都不适用: for (std::string line; std::getline(std::cin, line);) { std::cout << line << std::endl; } r

我有一个名为example.cpp的cpp程序。在这个程序中,我想逐行读取标准数据,并将每一行保存在一个字符串中。但是,它不是来自关键字或文件。输入来自命令行:

CC example.cpp -o ALO
last -n 10 | ./ALO
我尝试了以下几种方法,但都不适用:

for (std::string line; std::getline(std::cin, line);) {
    std::cout << line << std::endl;
}


return 0;
期望输出:

bkg988    sshd         139.218.157.100  Thu Mar 27 20:00 - 20:00  (00:00)
sh615     sshd         cpe-121-216-173- Thu Mar 27 19:57 - 19:59  (00:01)
sh615     sshd         cpe-121-216-173- Thu Mar 27 19:57 - 19:59  (00:01)

无论是从文件、终端还是管道读取,stdin的工作原理都是一样的。我认为最后一个版本应该可以工作。我已经编写了输出示例。第一个版本和第三个版本是等效的(除了
“get”
输出),它们应该可以工作。您是否可以将输出发送到一个文件,并验证它实际上不包含这些换行符?如果您使用
cin>>name
而不是
getline
,您会得到这样的结果。我将其发送到一个文本文件,它是逐行的:(
string name;
while (getline(cin, name))
{
    cout << "Got " << name << endl;
}
Got sshd
Got 192.131.251.2
Got Thu
Got Mar
Got 27
Got 18:16
Got -
Got 18:17
Got (00:00)
Got dwm914
Got sshd
Got mega-pc21.cs.uow
Got Thu
Got Mar
Got 27
Got 18:16
Got -
Got 18:17
Got (00:01)
Got tb713
Got pts/71
Got 192.131.251.2
Got Thu
Got Mar
Got 27
Got 18:15
Got -
Got 18:16
Got (00:01)
Got tb713
Got sshd
Got 192.131.251.2
Got Thu
Got Mar
Got 27
Got 18:15
Got -
Got 18:16
Got (00:01)
bkg988    sshd         139.218.157.100  Thu Mar 27 20:00 - 20:00  (00:00)
sh615     sshd         cpe-121-216-173- Thu Mar 27 19:57 - 19:59  (00:01)
sh615     sshd         cpe-121-216-173- Thu Mar 27 19:57 - 19:59  (00:01)