I have redirected the program's standard output to pipe. The only output are random 0 - 10^9 integers, up to 10 digits. In the other program I am reading the pipe.
Now, my problem is the handling the received data. If I define the char
buffer at length 10 + 1, then if the sent number has less than 10 digits the data starts getting messy. I receive some kind of trash, extra zeroes etc.
Since the standard output sends the data as a text, I can't use the int
as the buffer, since it takes only 4 bytes so the first 4 digits and cuts the rest of the number;
My question is: is there a possible solution for that?
I printed an output of the buffer. I have sent 10x 1000
, the result is:
31 30 30 30 0a 31 30 30 30 0a 31
30 30 30 0a 31 30 30 30 0a 31 30
30 30 0a 31 30 30 30 0a 31 30 30
30 0a 31 30 30 30 0a 31 30 30 30
0a 31 30 30 30 0a 0a 31 30 30 30
So all lines have 11 bytes as intended. But there is a carriage return at line 1 in the middle and then the next number appears. I guess the arithmetic to count the bytes until the carriage return and then taking the next few bytes as a number until another carriage return would get a bit ugly.
Maybe the way that I took is wrong, like there is no clear solution for the problem and it would be better to take another approach? My intention was to write a tester program for an C++ app. The tester sends the data line by line, and then takes the output from the C++ program line by line too and checks if the returned output is correct. The C++ app takes the input data only by standard input / output, that's the main limitation. It's pretty easy to accomplish this in other languages but here it got little messy I guess.
Looks like your data is one number per line (with 0x0a
to split the lines). So simply read a line at a time.
int main()
{
std::string line;
while(std::getline(std::cin, line)) {
std::cout << "Got Line: " << line << "\n";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.