pipe - piping a cat command to c++ code multiple times -
if want read file multiple times command ./run in.txt
, there straight way...
0- ofstream fin;
1- while (fin >> i) {...}
2- fin.clear(); fin.seekg(0);
3- while (fin >> i) {...}
however, want pipe zipped input file program. command is
bzcat file.bz2 | ./run
and in code, instead of ofstream fin
, have use std::cin
.
while (std::cin >> i) {...}
the loop terminates when bzcat sends last line program. now... question is, how can go start position , tell bzcat please send lines 1 more time (form first end) , catch them std::cin
?!
the pipe contents consumed read, need retain values in memory if need multipass algorithm, can uncompress , run on file or else can split program multiple stages , run them 1 after other:
// 1 std::vector<int> data; std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(data)); // operate on data #2 $ bunzip2 file.bz2 && ./run file.bz2 # 3 $ bzcat file.bz2 | ./stage1 | ./stage2 ...
Comments
Post a Comment