views:

94

answers:

3

Respected sirs,

My name is @nimit. I want to create a batch file and run it in a DOS prompt. The batch file will execute a C++ program I've written. The output should be stored in a single text-file. How can I do this?

The C++ program output should be stored in a particular text file.

Thanks in advance, @nimit

+1  A: 

You can do this:

programname > outputgoeshere.txt

To collect outputs:

programname1 >> outputgoeshere.txt
programname2 >> outputgoeshere.txt
programname3 >> outputgoeshere.txt
GMan
Probably append rather than overwrite?
dirkgently
but i want create batch file then it output goes in a text file in a dossuppose i have 3 c++ program that output store in a single text file
naval Parekh
appending would use >> instead of single > (which is overwrite)
MarceloRamires
Yes, then use the append.
GMan
but how to create the batch file in dos
naval Parekh
Try either `edit batchfile.bat` or `copy con batchfile.bat`. The latter will let you enter the file line by line. When you're done, press Ctrl+Z and then enter.
GMan
@user - put those statements inside a text-file with an extension of .bat, and you'll have your batch file.
sheepsimulator
A: 

Shell scripting (Batch files are a form of that) is something that every programmer should know how to do. I found a really great book on it a few years ago: Unix Shell Programming by Stephen Kochan and Patrick Wood. Granted, it's Unix -- and bash is far more powerful than DOS, but the principles are the same. Windows is picking up a lot of the tools that bash offers with powershell.

For a great website that lists out all of the CMD programs, visit http:// ss64.com/nt/ . That site also lists comparable bash and powershell commands. I also like how he shows you how to implement pseudo-functions, command line parameters, and all manner of cool things in batch files: http://ss64.com/nt/syntax.html

Good luck!

misterich
A: 

The following will redirect program output (stdout) to a file (overwrite file or create it if it does not exist)

$ command-name > output.log

The following will redirect program output (stdout) to a file (append file or create it if it does not exist)

$ command-name >> output.log
$ command-name >> output.log

The following will redirect program error message to a file called error.log:

$ command-name 2> error.log

Redirecting the standard error (stderr) and stdout to file, Use the following syntax:

$ command-name &> output_error.log
Phong