Many Command Prompt commands, and DOS commands for that matter, are executed not just to do something, but to provide you with information.
The ping command, dir command, tracert command, and several others might come to mind when you think of popular commands that produce a lot of data in the Command Prompt window.
Unfortunately, three hundred lines of information from the dir command doesn't do you much good as it rushes by. Yes, the more command can be helpful here, but what if you want to look at the output later, or send it to a tech support group, or use it in a spreadsheet, etc.?
The file must exist. 'w' Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. 'a' Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. OUPUT If successful, returns a pointer to a FILE object.
This is where a redirection operator becomes very useful. Using a redirection operator, you can redirect the output of a command to a file. It's one of our favorite Command Prompt Tricks & Hacks.
In other words, all the information that's displayed in the Command Prompt after running a command can instead be saved to a file which you can open in Windows to reference later or manipulate however you like.
While there are several redirection operators, which you can read in detail about here, two, in particular, are used to output the results of a command to a file: the greater-than sign, >, and the double greater-than sign, >>.
How to Use Redirection Operators
The easiest way to learn how to use these redirection operators is to see some examples:
In this example, all the network configuration information normally seen on screen after running ipconfig /all, is saved to a file by the name of mynetworksettings.txt. It's stored in the folder to the left of the command, C:Usersjonfi in this case.
As you can see, the > redirection operator goes between the ipconfig command and the name of the file you want to store the information in. If the file already exists, it'll be overwritten. If it doesn't already exist, it will be created.
Although a file will be created if doesn't already exist, folders will not. To save the command output to a file in a specific folder that doesn't yet exist, first create the folder and the run the command. You can make folders without leaving Command Prompt with the mkdir command.
Here, when the ping command is executed, Command Prompt outputs the results to a file by the name of Ping Results.txt located on the jonfi user's desktop, which is at C:UsersjonfiDesktop. The entire file path in wrapped in quotes because there was a space involved.
Remember, when using the > redirection operator, the file specified is created if it doesn't already exist and is overwritten if it does exist.
This example uses the >> redirection operator which functions in much the same way as the > operator, only instead of overwriting the output file if it exists, it appends the command output to the end of the file.
So, let's say the first time you use this command is on Computer A. The officenetsettings.log file is created and the result of ipconfig /all on Computer A is written to the file. Next you run the same command on Computer B. This time, however, the result is added to the officenetsettings.log so the network information from both Computer A and Computer B is included in the file.
Here's an example of what this LOG file might look like after a command has been exported to it:
As you might have already realized, the >> redirection operator is really useful when you're collecting similar information from multiple computers or commands and you'd like all of that data in a single file.
The above redirection operator examples are within the context of Command Prompt, but you can also use them in a BAT file. When you use a BAT file to pipe a command's output to a text file, the exact same commands described above are used, but instead of pressing Enter to run them, you just have to open the .BAT file.
The above is an example of how to make a batch file that uses a redirection operator with the tracert command.
The yahootracert.txt file (shown above) will be created on the Z: drive several seconds after executing the sample.bat file. Like the examples above, the file shows everything Command Prompt would have revealed if the redirection operator wasn't used.
Active9 years, 6 months ago
If I had an array of integers I wanted to output to a text file..
How can I save the output() to text file using the method below?
anon
tm1tm1
4 Answers
As an alternative to creating two functions (as per AareP's answer), you could add a destination parameter to the
output()
function:You can then reimplement the original
output()
as a simple wrapper:This might not seem too useful in this case, but I've put it here because this general pattern can be useful when the logic of the output function is more complicated. In general it's better to keep it in one place than to replicate it across multiple functions (it means you only have to fix bugs in one place, for one thing).
Community♦
cafcaf199k3131 gold badges262262 silver badges400400 bronze badges
Adding to what others have told:
You must always check for errors after
codaddictcodaddictfopen
, which on error return NULL
.367k7070 gold badges451451 silver badges494494 bronze badges
AarePAareP1,97433 gold badges1818 silver badges2828 bronze badges
If you want to reuse your output function you can do something like this.Using the a_output function you will be able to print the results both in console, or any other output stream.
Andrei CiobanuAndrei Ciobanu6,0042020 gold badges6363 silver badges110110 bronze badges