5

I am coding in C and running the programs in a Linux terminal. I want to save the output of the program in a .txt file. I usually do this by executing ./a.out>filename.txt

But I have written an interactive program in which I have to enter a number to initiate the program. In that case how do I do it?

Thanks a lot and your valuable suggestions are most welcomed.

4 Answers 4

8

Try this

./a.out | tee filename.txt

Tee man page

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't allow access to stdin for inputting the args
actually ur suggestion works when the code is not interactive but when it requires some input from our side..it dsnt word...anyways thnx for your reply
thnx finally it worked coz of ur suggestion actually i made mistake while typing innitially..thanx buddy once again
7

You can use script to capture all output to a file.

$ man script

Comments

6

Move the requirement to enter a number from the terminal to a command line parameter.

./a.out 42 > filename.txt

Or, easier, accept the input from a redirected input

echo 42 | ./a.out > filename.txt
./a.out < input.txt > filename.txt

Comments

1

Assuming you have enter the number you wish to pass the to the program in an file called 'input.txt'. If you want to re-direct the output to 'output.txt', then type at the command-line:

./a.out  <  input.txt  >  output.txt

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.