Linux libc-doc
Files | |
file | stdio.h |
Functions | |
int | printf (const char *format,...) |
Print formatted data to stdout More... | |
int | fprintf (FILE *stream, const char *format,...) |
Print formatted data to stream . More... | |
int | fgetc (FILE *stream) |
Read a character. More... | |
char * | fgets (char *s, int size, FILE *stream) |
Read a string. More... | |
int | fputc (int c, FILE *stream) |
Write a character. More... | |
int | fputs (const char *s, FILE *stream) |
Write a string. More... | |
void | perror (const char *s) |
Print an error message. More... | |
int | feof (FILE *stream) |
Test end-of-file indicator of a file stream. More... | |
int | ferror (FILE *stream) |
Test error indicator of a file stream. More... | |
Detailed Description
This code snippet illustrates the usage of fgetc() and fputc():
This code snippet illustrates the usage of fgets() and fputs():
Function Documentation
int printf | ( | const char * | format, |
... | |||
) |
The printf() (print formatted) function produces output according to a format string as specified in format
and writes it stdout
. The format string defines the structure of the output (i.e., how many and which additional arguments) and the additional parameter of the printf() function are used to replace the arguments in the format string with actual data. It is important that the number of additional parameter matches the number of arguments of the format string.
An argument in the format string starts with a %
. The following table shows some important arguments, however, there are several more options described in the manpage of printf() (man 3 printf
):
Type | Description |
---|---|
i , d | integer |
u | unsigned integer |
f | floating point |
p | pointer |
s | string |
c | character |
Example:
We do not expect error handling for printf().
- Parameters
-
format format string ... arguments
- Return values
-
>=0 number of characters printed <0 on error, errno
is set
int fprintf | ( | FILE * | stream, |
const char * | format, | ||
... | |||
) |
The fprintf() (file stream print formatted) function is very similar to printf(), except that it does not write to stdout
, but to stream
.
For more details see printf().
We do not expect error handling for fprintf().
- Parameters
-
stream file stream to write format format string, see printf() ... arguments
- Return values
-
>=0 number of characters printed <0 on error, errno
is set
int fgetc | ( | FILE * | stream | ) |
The fgetc() (file stream get character) function returns an unsigned char
casted to an int read from a stream
or returns EOF
.
fgetc() returns EOF
on error or when the end of the file is reached. To distinguish these two events the feof() or ferror() function can be used. Be aware, that the return value of fgetc() must be saved in an int
(and not in an unsigned char
) in order to distinguish EOF
and 0xFF (which is the character ÿ
when interpreted as ISO-8859-1).
- Parameters
-
stream file stream to read
- Return values
-
EOF on error or end of file, errno
is set on error!=EOF unsigned char
read fromstream
char* fgets | ( | char * | s, |
int | size, | ||
FILE * | stream | ||
) |
The fgets() (file stream get string) function reads at most size-1
characters from stream
and saves them in the buffer pointed to by s
. It terminates the string in s
with a null byte (\0
).
fgets() reads in characters until it finds a newline character (\n
) or it has read size
-1 characters. If an error occurred or the end of file was reached it returns NULL
. The ferror() and feof() functions can be used to distinguish these two events.
- Parameters
-
s buffer to write to size size of the buffer stream file stream to read
- Return values
-
NULL on error or end of file, errno
is set on error!=NULL pointer to s
int fputc | ( | int | c, |
FILE * | stream | ||
) |
int fputs | ( | const char * | s, |
FILE * | stream | ||
) |
void perror | ( | const char * | s | ) |
The perror() (print error) function produces an error message on stderr
printing the string in s
followed by a human-readable description of the value in errno
. This is especially helpful after a failed call to a system or library function, which sets the errno
variable (e.g., malloc()).
- Parameters
-
s string printed before the actual error message
int feof | ( | FILE * | stream | ) |
The feof() (file stream end of file) function tests the EOF
indicator of stream
.
- Parameters
-
stream file stream to test
- Return values
-
0 end-of-file indicator is not set !=0 end-of-file indicator is set
int ferror | ( | FILE * | stream | ) |
The ferror() (file stream error) function tests the error indicator of stream
.
- Parameters
-
stream file stream to test
- Return values
-
0 error indicator is not set !=0 error indicator is set