0

I have this piece of code that produces some output with printf statements on the console. I want to somehow collect the data into a .txt file when I run this code multiple times with different variables, for example if my program is called "produce", I want to get the outputs of:

./produce 0
./produce 1
./produce 2

and so on. Any way of automating this? Thanks.

4
  • 4
    Write a shell script to run it multiple times. Redirect the output to a specific text file.
    – halfelf
    Oct 30, 2012 at 2:40
  • I've never used shell before, can you give me an example? Oct 30, 2012 at 2:53
  • check this tldp.org/LDP/abs/html And the most easy one is just write the command one by one.
    – halfelf
    Oct 30, 2012 at 2:57
  • 1
    Look at the "Redirection" section in the Bash manual page. Oct 30, 2012 at 7:13

2 Answers 2

1

You can write a simple shell script for this.

for i in 1 2 3 4 5 6 7 8 9 10
do
echo "Output of produce for $i" >> output.txt
./produce $i >> output.txt
done

Put the above code in a file like test.sh. And then run the command sh test.sh on the console and you should be seeing the file output.txt getting created with the desired output in it.

Make sure to rename output.txt before running the script everytime.

2
  • Thanks, this did it but I had to change the ./produce i to ./produce $i Oct 30, 2012 at 15:00
  • Ya. That was a typo. I'll correct it.Hope it helped you solve the problem.
    – CCoder
    Oct 30, 2012 at 15:10
0

If you are nervous of the shell, then just modiify your code to write to a file which is named using the input parameter.

See this page for how to write to a file.

FILE *outputFile;
outputFile = fopen(strcat(strcat('output_', argv[1]), '.txt, "wt");  // output_0.txt, output_1.txt, etc

Replace all printf( with fprintf(outputFile, and that's it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.