【发布时间】:2013-03-01 22:24:41
【问题描述】:
假设我在下面有这段代码,我用它来运行像ls 这样的简单命令,并假设输出是100 行长。
当我们在下面的代码中执行cout << buff 时,输出是逐字节、逐位还是逐行进行流式传输,或者间隔是多少?
假设我们要在流式传输cout<<buff 时打印每个新行的new_line! 字符串结尾。我们该怎么做?
我在想,在 while 循环中,我们应该有一些东西来检查它是否刚刚流式传输(或即将流式传输)\n 到我们的字符串流 output,并且它决定在之后(或之前)做一些事情) 它流入输出。
最初我将把它变成数据结构来存储,所以我只需要知道如何检查\n,因为我们正在缓冲,以及在哪里放置添加new_line!的函数或我想做的任何事情。
代码如下:
#include <iostream>
#include <stdio.h>
using namespace std;
// imaginary
// stringstream output;
int main() {
FILE *in;
char buff[512];
if(!(in = popen("ls -sail", "r"))){
return 1;
}
while(fgets(buff, sizeof(buff), in)!=NULL){
// output << buff;
// if there is a new line
// output << "new_line!";
cout << buff;
}
pclose(in);
return 0;
}
【问题讨论】: