Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

phil05

macrumors newbie
Original poster
Feb 4, 2009
2
0
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

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
Code:
./hello > results.txt
 

foidulus

macrumors 6502a
Jan 15, 2007
904
1
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
 

bookshadow

macrumors newbie
Jan 16, 2009
1
0
Doing it in code

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;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.