Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a quick question:

I have a C++ program that prompts the user for inputs, accepts those inputs, does things based on the inputs, and displays the results (everything is being displayed to the shell window using printf). I want to take everything that is being displayed in the shell window and instead write it to a file. If I could make it display in the shell window and also write to the file, that would be even better. Here is what I attempted to do:

program.out>programresult.txt

I also tried:

program.out>>programresult.txt

Both of these commands do absolutely nothing. The cursor just goes to the next line and sits there (without even a command prompt). The .out file does not run at all. Please help me do this, or let me know if it cannot be done. Thank you.

P.S. The file 'programresult.txt' is created in the directory, but the file is empty.

share|improve this question
4  
This would be better suited to Stack Overflow. Also, you should post your source code. – dv3500ea Feb 26 '11 at 9:42
Well, I had an answer for you -- but by the time I created an account and typed up my answer, the question had been closed. Post your question on stackoverflow.com (which is the same kind of site but for programming-specific questions such as yours). – nobar Feb 26 '11 at 19:33
You might try using tee... program.out |tee programresult.txt This should allow program output to go to both the file and the shell window. This is just a guess, but maybe the reason nothing happens is because your program is waiting for user input. Also, maybe the reason you don't see anything in the file is because the results are being buffered up to reduce file I/O overhead (which is not helpful in this case). You can defeat the buffering by using fflush(): printf("Type user input now: "); fflush(stdout); fscanf(...); – nobar Feb 26 '11 at 19:34

closed as off topic by htorque, dv3500ea, João Pinto, DoR, hhlp Feb 26 '11 at 19:10

Questions on Ask Ubuntu are expected to relate to Ubuntu within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

well, if you're using C++, use cout instead of printf. Anyway, following code works:

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    cout << "blahblah" << endl;

    return EXIT_SUCCESS;
}

Now, g++ file.cpp -o program_name

And ./program_name > filename.txt produces filename.txt with "blahblah" inside.

share|improve this answer
Thanks for the response, but actually for this class I'm in I have to use just C (I'm just using the C++ compiler). Anyway, I think the issue lies in the fact that my program has to take inputs, but I'm not positive...any ideas? – user11502 Feb 26 '11 at 19:05

If you want to leave your code unedited, and if there is already a print statement inside your code what you can do is pipe standard out and standard error to a file. like so:

command-name &>file

That should work without having to edit your C code. But you have user prompts, which would require a change in your code, depending on what you need. If it is just output saying that is the command line mojo you need.

share|improve this answer
Yeah, that still doesn't work. My program does take input from the user (an indefinite amount, until the user decides to quit), so are you saying this is not possible to do with a program that has to accept input? – user11502 Feb 26 '11 at 19:10
@drew you will need to rewrite your code so that the result is stored in the the file. – myusuf3 Feb 26 '11 at 20:15