Sometimesit is desirable to redirect the output from the standard output device to a file. However, you may still want the error messages to go to the standard output device, in our case the monitor. This next function allows you to do that in MS-DOS systems. Load and display special.c for an example of this new function.
#include "/sys/stdio.h"
main( )
{
int index;
for (index = 0;index < 6;index++) {
printf("This line goes to standard output.\n");
fprintf(stderr,"This line goes to the error device.\n");
}
exit(4);
/* This can be tested with the MS-DOS errorlevel
command in a batch file. The number returned
is used as follows;
IF ERRORLEVEL 4 GOTO FOUR
(continue here if less than 4)
.
.
GOTO DONE
:FOUR
(continue here if 4 or greater)
.
.
:DONE
*/
}
The program consists of a loop with two messages output, one to the standard output device and
the other to the standard error device. The message to the standard includes the device name
"stderr" as the first argument. Other than those two small changes, it is the same as our standard
"printf" function. (You will see moreof the "fprintf" function in the next chapter, but its operation
fit in better as a part of this chapter.) Ignore the line with the "exit" for the moment, we will
return to it.
Compile and run this program, and you will find 12 lines of output on the monitor. To see the
difference, run the program again with redirected output to a file named "STUFF" by entering
the following line at a 1616/OS prompt;
F0/ special >stuff
Standard Input/Output C Tutorial 9-8
More information about I/O redirection can be found in your 1616/OS Users Manual. This time
you will only get the 6 lines output to the standard error device, and if you look in your directory,
you will find the file named "STUFF" containing the other 6 lines, those to the standard output
device. You can use I/O redirection with any of the programs we have run so far, and as you
may guess, you can also read from a file using I/O redirection but we will study a better way to
read from a file in the next chapter.
#include "/sys/stdio.h"
main( )
{
int index;
for (index = 0;index < 6;index++) {
printf("This line goes to standard output.\n");
fprintf(stderr,"This line goes to the error device.\n");
}
exit(4);
/* This can be tested with the MS-DOS errorlevel
command in a batch file. The number returned
is used as follows;
IF ERRORLEVEL 4 GOTO FOUR
(continue here if less than 4)
.
.
GOTO DONE
:FOUR
(continue here if 4 or greater)
.
.
:DONE
*/
}
The program consists of a loop with two messages output, one to the standard output device and
the other to the standard error device. The message to the standard includes the device name
"stderr" as the first argument. Other than those two small changes, it is the same as our standard
"printf" function. (You will see moreof the "fprintf" function in the next chapter, but its operation
fit in better as a part of this chapter.) Ignore the line with the "exit" for the moment, we will
return to it.
Compile and run this program, and you will find 12 lines of output on the monitor. To see the
difference, run the program again with redirected output to a file named "STUFF" by entering
the following line at a 1616/OS prompt;
F0/ special >stuff
Standard Input/Output C Tutorial 9-8
More information about I/O redirection can be found in your 1616/OS Users Manual. This time
you will only get the 6 lines output to the standard error device, and if you look in your directory,
you will find the file named "STUFF" containing the other 6 lines, those to the standard output
device. You can use I/O redirection with any of the programs we have run so far, and as you
may guess, you can also read from a file using I/O redirection but we will study a better way to
read from a file in the next chapter.

