PDA

View Full Version : Mac terminal




phil05
Feb 4, 2009, 05:19 PM
I have compiled a C++ program in the terminal and i want to output the results to a text file (results.txt) instead of the terminal.
What command do i use to do this?

Thanks



toddburch
Feb 4, 2009, 05:25 PM
I have compiled a C++ program in the terminal and i want to output the results to a text file (results.txt) instead of the terminal.
What command do i use to do this?

Thanks

Run your program and redirect it's output to a file. If you program is "hello", type

./hello > results.txt

MacRumors Guy
Feb 4, 2009, 05:26 PM
./your_program_name > file.txt or >> if you want to append to the file.

phil05
Feb 4, 2009, 05:33 PM
Thanks for the help guys.

foidulus
Feb 5, 2009, 06:53 AM
You can also pipe it to tee if you want to see the results on screen and store it in a file:

./your_program | tee output_file_name.txt

toddburch
Feb 5, 2009, 07:45 AM
Cool - I wasn't aware of tee.

Too many goodies in unix to learn them in one sitting.

bookshadow
Feb 7, 2009, 12:22 PM
Why don't you do it in the code of your program?


#include <stdio.h>
#include <stdlib.h>

// create a file pointer and the string to print to the file
FILE *fp;
char buffer[] = "This is my result string";

// open the file and exit() from stdlib.h if it does not exist
fp = fopen("results.txt","w");
if (fp == NULL) exit(1);

// print the string to the file
fprintf(fp,"result: %s", buffer);

// close the file to save the changes
fclose(fp);

return 0;