| << Prev | Beej's Guide to C | Next >> |
The most basic of all libraries in the whole of the standard C library is the standard I/O library. It's used for reading from and writing to files. I can see you're very excited about this.
So I'll continue. It's also used for reading and writing to the console, as we've already often seen with the printf() function.
(A little secret here--many many things in various operating systems are secretly files deep down, and the console is no exception. "Everything in Unix is a file!" :-))
You'll probably want some prototypes of the functions you can use, right? To get your grubby little mittens on those, you'll want to include stdio.h.
Anyway, so we can do all kinds of cool stuff in terms of file I/O. LIE DETECTED. Ok, ok. We can do all kinds of stuff in terms of file I/O. Basically, the strategy is this:
What's in the
This is actually pretty important: try to not muck around in the
One more thing to mention about the standard I/O library: a lot of the functions that operate on files use an "f" prefix on the function name. The same function that is operating on the console will leave the "f" off. For instance, if you want to print to the console, you use printf(), but if you want to print to a file, use fprintf(), see?
Wait a moment! If writing to the console is, deep down, just like
writing to a file, since everything in Unix is a file, why are there two
functions? Answer: it's more convenient. But, more importantly, is
there a
There are, in fact, three (count 'em!) special
That hardly seems fair--why does output get two files, and input only get one?
That's jumping the gun a bit--let's just look at them:
Input from the console.
Output to the console.
Output to the console on the error file stream.
So standard input (stdin) is by default just what you type at the keyboard. You can use that in fscanf() if you want, just like this:
/* this line: */
scanf("%d", &x);
/* is just like this line: */
fscanf(stdin, "%d", &x);
And stdout works the same way:
printf("Hello, world!\n");
fprintf(stdout, "Hello, world!\n"); /* same as previous line! */
So what is this stderr thing? What happens when you output to that? Well, generally it goes to the console just like stdout, but people use it for error messages, specifically. Why? On many systems you can redirect the output from the program into a file from the command line...and sometimes you're interested in getting just the error output. So if the program is good and writes all its errors to stderr, a user can redirect just stderr into a file, and just see that. It's just a nice thing you, as a programmer, can do.
| << Prev | Beej's Guide to C | Next >> |