This site is sponsored by Data Graphics, Inc the world leader in Custom Labels, Nameplates and Industrial Printing. Serving the Industrial Printing needs of corporate, Military, and Aerospace clients worldwide for 25 years. If you need custom labels, and you need the fast, and correct the first time, then you need Data Graphics.
What’s the difference between printing to standard output and printing to standard error?
In C, how would you go about doing both of those? Is it printf and something else for printing to error- do you just use the same thing…
When a C program starts up, the run time automatically opens 3 file handles for it:stdin, stdout and stderr. These really are just file handles, as in FILE *. Normally, stdin is connected to the keyboard, and stdout and stderr are connected to the display console. But if you redirect stdout to a file, then stdout goes to the file, while stderr will still go to the screen.
Since they really are just FILE * file handles, you can use them in any function that takes a FILE * parameter.
printf doesn’t take a FILE *, but fprintf does. These 2 function calls are identical:
printf("Hello World\n");
fprintf(stdout,"Hello World\n");
For most functions that read the keyboard or write to the screen, there is another function of the same name, but starting with f… puts() –> fputs(). gets() –> fgets(), etc.
If you were to do this:
fprintf(stderr,"Saying Hello World\n");
printf("Hello World\n");
and run it as:
sayhello > hello.txt
Then you should see ‘Saying Hello…’ on the screen, and have ‘Hello World’ in hello.txt file.
Sorry if that was too much detail
One Response to “What’s the difference between printing to standard output and printing to standard error?”
Leave a Reply
This site is sponsored by Data Graphics, Inc the world leader in Custom Labels, Nameplates and Industrial Printing. Serving the Industrial Printing needs of corporate, Military, and Aerospace clients worldwide for 25 years. If you need custom labels, and you need the fast, and correct the first time, then you need Data Graphics.
When a C program starts up, the run time automatically opens 3 file handles for it:stdin, stdout and stderr. These really are just file handles, as in FILE *. Normally, stdin is connected to the keyboard, and stdout and stderr are connected to the display console. But if you redirect stdout to a file, then stdout goes to the file, while stderr will still go to the screen.
Since they really are just FILE * file handles, you can use them in any function that takes a FILE * parameter.
printf doesn’t take a FILE *, but fprintf does. These 2 function calls are identical:
printf("Hello World\n");
fprintf(stdout,"Hello World\n");
For most functions that read the keyboard or write to the screen, there is another function of the same name, but starting with f… puts() –> fputs(). gets() –> fgets(), etc.
If you were to do this:
fprintf(stderr,"Saying Hello World\n");
printf("Hello World\n");
and run it as:
sayhello > hello.txt
Then you should see ‘Saying Hello…’ on the screen, and have ‘Hello World’ in hello.txt file.
Sorry if that was too much detail
References :