Lets say I have this piece of code below and I use it to run a simple command like ls
and lets assume that the output is 100
lines long.
When we are doing cout << buff
in the code below, does the output get streamed byte by byte, bit by bit, or line by line or what is the interval?
Lets say we want to print new_line!
string end of every new line as we are streaming cout<<buff
. How can we do so ?
I'm thinking in that while
loop we should have something that should check that it just streamed (or about to stream) \n
into our stringstream output
, and it decides to do something after (or before) it streams into the output.
Initially I will just make this into data structure to store so I just need to know how to check for \n
as we are bufferring and where to put the function that adds new_line!
or whatever I want to do.
Here's the code:
#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;
}