How do the C stream system works?
For example, the code:
FILE *f;
// opens f...
fputc(f, "x");
will do different things, depending on how ‘f’ was open. If ‘f’ was open as a file, a character will be written in that file. If ‘f’ was open as a memory stream, a char will be written in the memory, and possibly more memory will be alloced.
So my question is: how does the fputc function knows what ‘f’ means and what to do with it?
Aditional question: does it depend entirely on the operating system or is it possible to implement something a abstract interface like this in pure C?
The open function stores that information inside the
FILEstructure thatfpoints to. It’s pure C, though the low-level code to do the writing to the file will be platform-dependent.