| |||
| Home > The C and C++ Libraries > Tailoring the input/output functions > Dependencies on low-level functions | |||
Table 5.15 shows
the dependencies of the higher-level function 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 5.15. 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 | - | - | - | - |
[1] The file structure. [2] The standard input object of type [3] The standard output object of type [4] Outputs a character to a file. [5] Returns the error status accumulated during file I/O. [6] Gets a character from a file. [7] Moves file pointer to previous character. See Re-implementing __backspace(). | |||||||||||
See the ISO C Reference for the syntax of the low-level functions.
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.
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.
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(),
and puts() unchanged from the library. Example 5.19 shows how to do
this. Consider modifying the system routines if you require real
file handling.
Example 5.19. printf() and __FILE
#include <stdio.h>
struct __FILE {
int handle;
/* Whatever you need here (if the only files 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. See also the implementation
in the main examples directory, in ...\emb_sw_dev\source\retarget.c.
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 directly
from the library.
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.
__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
assuming that this will not happen.