2.12.1. Dependencies on low‑level functions
Table 2.13 shows the dependencies of the higher‑level functions on lower‑level functions. If you define your own versions of the lower‑level functions, you can use the library versions of the higher‑level functions directly. fgetc() uses __FILE, but fputc() uses __FILE and ferror().
Table 2.13. Input/output dependencies
| Low‑level object | | | | | High‑level function | | | | |
|---|
| | fprintf | printf | fwrite | fputs | puts | fscanf | scanf | fread | read | fgets | gets |
__FILE[1] | x | x | x | x | x | x | x | x | x | x | x |
__stdin[2] | ‑ | ‑ | ‑ | ‑ | ‑ | ‑ | x | ‑ | x | ‑ | x |
__stdout[3] | ‑ | x | ‑ | ‑ | x | ‑ | ‑ | ‑ | ‑ | ‑ | ‑ |
fputc()[4] | x | x | x | x | x | ‑ | ‑ | ‑ | ‑ | ‑ | ‑ |
ferror()[5] | x | x | x | ‑ | ‑ | ‑ | ‑ | ‑ | ‑ | ‑ | ‑ |
fgetc()[6] | ‑ | ‑ | ‑ | ‑ | ‑ | x | x | x | x | x | x |
__backspace()[7] | ‑ | ‑ | ‑ | ‑ | ‑ | x | x | ‑ | ‑ | ‑ | ‑ |
See the ISO C Reference for the syntax of the low‑level functions.
Note
If you choose to re‑implement fgetc(), fputc(), and __backspace(), be aware that fopen() and related functions use the ARM layout for the __FILE structure. You might also need to re-implement fopen() and related functions if you define your own version of __FILE.
The printf family consists of _printf(), printf(), _fprintf(), fprintf(), vprintf(), and vfprintf(). All these functions use __FILE opaquely and depend only on the functions fputc() and ferror(). The functions _printf() and _fprintf() are identical to printf() and fprintf() except that they cannot format floating‑point values.
The standard output functions of the form _printf(...) are equivalent to:
fprintf(& __stdout, ...)
where __stdout has type __FILE.
The scanf() family consists of scanf() and fscanf(). These functions depend only on the functions fgetc(), __FILE, and __backspace(). See Re‑implementing __backspace().
The standard input function of the form scanf(...) is equivalent to:
fscanf(& __stdin, ...)
where __stdin has type __FILE.
fwrite(), fputs, and puts
If you define your own version of __FILE, and your own fputc() and ferror() functions and the __stdout object, you can use all of the printf() family, fwrite(), fputs(), puts() and the C++ object std::cout unchanged from the library. Example 2.24 shows how to do this. Consider modifying the system routines if you require real file handling.
Example 2.24. printf() and __FILE
#include <stdio.h>
struct __FILE
{
int handle;
/* Whatever you need here (if the only file you are using
is the stdoutput using printf for debugging, no file
handling is required) */
};
FILE __stdout;
int fputc(int ch, FILE *f)
{
/* Your implementation of fputc */
return ch;
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void test(void)
{
printf("Hello world\n"); /* This works ... */
}
By default, fread() and fwrite() call fast block input/output functions that are part of the ARM stream implementation. If you define your own __FILE structure instead of using the ARM stream implementation, fread() and fwrite() call fgetc() instead of calling the block input/output functions.
fread(), fgets(), and gets()
The functions fread(), fgets(), and gets() are implemented as a loop over fgetc() and ferror(). Each uses the FILE argument opaquely.
If you provide your own implementation of __FILE, __stdin (for gets()), fgetc(), and ferror(), you can use these functions, and the C++ object std::cin directly from the library.
Re‑implementing __backspace()
The function __backspace() is used by the scanf family of functions. It must never be called directly, but re‑implemented if you are retargeting the stdio arrangements at the fgetc() level.
The semantics are:
int __backspace(FILE *stream);
__backspace(stream) must be called after reading a character from the stream. It returns to the stream the last character that was read from the stream, so that the same character is read from the stream again. This means that a character that was read from the stream by scanf but which is not required (that is, it terminates the scanf operation) is read correctly by the next function that reads from the stream.
__backspace is separate from ungetc(). This is to guarantee that a single character can be pushed back after the scanf family of functions has finished.
The value returned by __backspace() is either 0 (success) or EOF (failure). It returns EOF only if used incorrectly, for example, if no characters have been read from the stream. When used correctly, __backspace() must always return 0, because the scanf family of functions do not check the error return.
The interaction between __backspace() and ungetc() is:
If you apply __backspace() to a stream and then ungetc() a character into the same stream, subsequent calls to fgetc() must return first the character returned by ungetc(), and then the one returned by __backspace().
If you ungetc() a character back to a stream, then read it with fgetc(), and then backspace it, the next character read by fgetc() must be the same character that was returned to the stream. That is the __backspace() operation must cancel the effect of the fgetc() operation. However, another call to ungetc() after the call to __backspace() is not required to succeed.
The situation where you ungetc() a character into a stream and then __backspace() another one immediately, with no intervening read, never arises. __backspace() must only be called after fgetc(), so this sequence of calls is illegal. You can write __backspace() implementations and assume that this does not happen.