I have been learning about pipes in C recently. Here is an example of some code that works fine:
Now this does exactly what it is supposed to do, last | sort , I'm trying to do all this with popen instead. Here is my attempt at that.
// some lines are omitted
execl("/usr/bin/last", "last", 0);
execl("/bin/sort", "sort", 0);
This almost works perfectly, except I get 10000 blank lines printing to stdout first. I know it's 10000 because I through in a counter at one point. After the blank lines it outputs semi-properly. every 10 lines or so a line that's not sorted properly appears. Thanks for any help/advice on this.
FILE *fr, *fw;
int i, j=0;
fr = popen("last", "r");
fw = popen("sort", "w");
while (fgets(buffer, sizeof(buffer), fr))
fprintf(fw, "%s\n", buffer);