Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Your last sentences imply that this is not reproducible, am I right? I propose to run this in a loop to figure out how often the problem actually occurs. –  Alfe Dec 11 '13 at 10:52
1  
If results aren't reproducible, I'd concur that something changed the source file between your two tries (ie. from cmd-line and system()). –  Sami Laine Dec 11 '13 at 11:37
    
You aren't checking the return value from system, which is bad. Also, your first use of cat is a useless one. –  John Zwinck Dec 11 '13 at 13:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.