Samsara
Samsara

Reputation: 37

using > instead of stdout

So for my program I need to take all the informations that are shown in cmd.exe and put them on a file. So for that I used the following code

freopen ("text.txt","w",stdout);

and I had to put it in the main.cpp

However I was told that I should do that in a diffrent class and that I could use the > symbol directly.

Could you guys tell me how I can do that?

If you could give me an example that would be great.

Upvotes: 1

Views: 157

Answers (2)

NtscCobalt
NtscCobalt

Reputation: 1689

std::cout << "Output sentence"; // prints Output sentence on screen
std::cout << 120;               // prints number 120 on screen
std::cout << x;                 // prints the content of x on screen 

If you use those then the user can redirect your output (which will normally go to the console) to a file instead by using the syntax below.

yourapplication.exe > "output.txt"

If you use std::cin with the << operator then you can also < "input.txt" to enter text from input.txt as if the user typed it.

http://www.cplusplus.com/doc/tutorial/basic_io/ explain the input and output streams fairly well.

http://technet.microsoft.com/en-us/library/bb490982.aspx explains console redirection.

Upvotes: 3

slaphappy
slaphappy

Reputation: 6999

I think you were told about pipes. In you shell, you can type something like:

somecommand > text.txt

And it will write the output of somecommand into text.txt.

Upvotes: 3

Related Questions