fprintf doesnt print to a file as such, but rather to a file descriptor. To print to a file you first need a descriptor that points to it.
Code:
#include <stdio.h>
int main() {
FILE *fd; /* File descriptor */
fd = fopen("myfile.txt", "r"); /* Point the file descriptor at a file */
fprintf(fd, "HelloWorld\n"); /* Print to the file descriptor */
}
stdio.h handily defines three file descriptors, stdin (standard input), stdout(standard output) and stderr(standard error). Its a bit confusing but printing to a file and printing to the screen are essentially the same thing. printf("HelloWorld\n"); is the same as writting printf(stdout, "HelloWorld\n");
By ensuring that your program prints errors to stderr you allow for greater flexibility when using your program. e.g.
Code:
$ ./myprog
myprog starting...
Error: Not enough memory
myprog ending...
$ ./myprog > stdout.txt 2> stderr.txt
$ cat stdout.txt
myprog starting...
myprog ending....
$ cat stderr.txt
Error: Not enough memory
The redirection operators in the shell can be used to redirect stdin, stdout and stderr (and other descriptors).
Na_th_an, yeah the two OSes do use a few gotos here and there. I removed the commands directory from MINIX because it contains user mode programs:
Code:
$ cd minix/src
$ grep -r goto * | grep -v commands | wc -l
96
$ cd /usr/src/linux
$ grep -r goto * | wc -l
14837
MINIX doesnt use them nearly as much as Linux does. Linux uses them primarly for speed because unconditional jumps are much quicker than both function calls and conditional jumps.
esus saves.... Passes to Moses, shoots, he scores!