Difference in output when executing system(...) in program and actual command

42 views Asked by At

I have a program written in C that creates an output file with lines of characters. My intention is to count the number of unique lines of characters in this output file (excluding "ABC").

I can do it manually via the Linux command line, using

cat output/output.txt | grep -v "ABC" | sort | uniq -c > uniq_stats/stats.txt

I also put this command into my program so I don't have to do it manually.

memset(command, 0, 500);
sprintf(command, "cat %s | grep -v \"ABC\" | sort | uniq -c > uniq_stats/%s", out_filename, filename);
system(command);

out_filename is output/output.txt and filename is stats.txt

I expect a particular line to be seen 1351 times. The method of using the command line gave this correct value. However, the system(command) method gave only 1349 times. Also, there was another line that was incomplete using the system(command) method, i.e. only a portion of the string was printed out.

Why is it that I got different output from the 2 methods? I have only seen this problem once, as I have tried 4 or 5 other files and both methods gave me the correct results.

0

There are 0 answers