cppreference.com > Standard C I/O clearerr Syntax:
      #include <stdio.h>
      void clearerr( FILE *stream );
The clearerr function resets the error flags and EOF indicator for the given stream. When an error occurs, you can use perror() to figure out which error actually occurred. Related topics: feof ferror perror

fclose
Syntax:

      #include <stdio.h>
      int fclose( FILE *stream );

The function fclose() closes the given file stream, deallocating any buffers associated with that stream. fclose() returns 0 upon success, and EOF otherwise.

Related topics:
fflush
fopen
freopen
setbuf

feof
Syntax:

      #include <stdio.h>
      int feof( FILE *stream );

The function feof() returns a nonzero value if the end of the given file stream has been reached.

Related topics:
clearerr
ferror
getc
perror
putc

ferror
Syntax:

      #include <stdio.h>
      int ferror( FILE *stream );

The ferror() function looks for errors with stream, returning zero if no errors have occured, and non-zero if there is an error. In case of an error, use perror() to determine which error has occured.

Related topics:
clearerr
feof
perror

fflush
Syntax:

      #include <stdio.h>
      int fflush( FILE *stream );

If the given file stream is an output stream, then fflush() causes the output buffer to be written to the file. If the given stream is of the input type, then fflush() causes the input buffer to be cleared. fflush() is useful when debugging, if a program segfaults before it has a chance to write output to the screen. Calling fflush( STDOUT ) directly after debugging output will ensure that your output is displayed at the correct time.

       printf( "Before first call\n" );
       fflush( STDOUT );
       shady_function();
       printf( "Before second call\n" );
       fflush( STDOUT );
       dangerous_dereference();

Related topics:
fclose
fopen
fread
fwrite
getc
putc

fgetc
Syntax:

      #include <stdio.h>
      int fgetc( FILE *stream );

The fgetc() function returns the next character from stream, or EOF if the end of file is reached or if there is an error.

Related topics:
fopen
fputc
fread
fwrite
getc
getchar
gets
putc

fgetpos
Syntax:

      #include <stdio.h>
      int fgetpos( FILE *stream, fpos_t *position );

The fgetpos() function stores the file position indicator of the given file stream in the given position variable. The position variable is of type fpos_t (which is defined in stdio.h) and is an object that can hold every possible position in a FILE. fgetpos() returns zero upon success, and a non-zero value upon failure.

Related topics:
fseek
fsetpos
ftell

fgets
Syntax:

      #include <stdio.h>
      char *fgets( char *str, int num, FILE *stream );

The function fgets() reads up to num - 1 characters from the given file stream and dumps them into str. fgets() will stop when it reaches the end of a line, in which case str will be terminated with a newline. If fgets() reaches num - 1 characters or encounters the EOF, str will be null-terminated. fgets() returns str on success, and NULL on an error.

Related topics:
fputs
fscanf
gets
scanf

fopen
Syntax:

      #include <stdio.h>
      FILE *fopen( const char *fname, const char *mode );

The fopen() function opens a file indicated by fname and returns a stream associated with that file. If there is an error, fopen() returns NULL. mode is used to determine how the file will be treated (i.e. for input, output, etc)

Mode | Meaning
--- | ---
"r" | Open a text file for reading
"w" | Create a text file for writing
"a" | Append to a text file
"rb" | Open a binary file for reading
"wb" | Create a binary file for writing
"ab" | Append to a binary file
"r+" | Open a text file for read/write
"w+" | Create a text file for read/write
"a+" | Open a text file for read/write
"rb+" | Open a binary file for read/write
"wb+" | Create a binary file for read/write
"ab+" | Open a binary file for read/write

An example:

       char ch;
       FILE *input = fopen( "stuff", "r" );
       ch = getc( input );

Related topics:
fclose
fflush
fgetc
fputc
fread
freopen
fseek
fwrite
getc
getchar
setbuf

fprintf
Syntax:

      #include <stdio.h>
      int fprintf( FILE *stream, const char *format, ... );

The fprintf() function sends information (the arguments) according to the specified format to the file indicated by stream. fprintf() works just like printf() as far as the format goes. The return value of fprintf() is the number of characters outputted, or a negative number if an error occurs. An example:

       char name[20] = "Mary";
       FILE *out;
       out = fopen( "output.txt", "w" );
       if( out != NULL )
         fprintf( out, "Hello %s\n", name );

Related topics:
fputc
fputs
fscanf
printf
sprintf

fputc
Syntax:

      #include <stdio.h>
      int fputc( int ch, FILE *stream );

The function fputc() writes the given character ch to the given output stream. The return value is the character, unless there is an error, in which case the return value is EOF.

Related topics:
fgetc
fopen
fprintf
fread
fwrite
getc
getchar
putc

fputs
Syntax:

      #include <stdio.h>
      int fputs( const char *str, FILE *stream );

The fputs() function writes an array of characters pointed to by str to the given output stream. The return value is non-negative on success, and EOF on failure.

Related topics:
fgets
fprintf
fscanf
gets
puts

fread
Syntax:

      #include <stdio.h>
      int fread( void *buffer, size_t size, size_t num, FILE *stream );

The function fread() reads num number of objects (where each object is size bytes) and places them into the array pointed to by buffer. The data comes from the given input stream. The return value of the function is the number of things read. You can use feof() or ferror() to figure out if an error occurs.

Related topics:
fflush
fgetc
fopen
fputc
fscanf
fwrite
getc

freopen
Syntax:

      #include <stdio.h>
      FILE *freopen( const char *fname, const char *mode, FILE *stream );

The freopen() function is used to reassign an existing stream to a different file and mode. After a call to this function, the given file stream will refer to fname with access given by mode. The return value of freopen() is the new stream, or NULL if there is an error.

Related topics:
fclose
fopen

fscanf
Syntax:

      #include <stdio.h>
      int fscanf( FILE *stream, const char *format, ... );

The function fscanf() reads data from the given file stream in a manner exactly like scanf(). The return value of fscanf() is the number of variables that are actually assigned values, or EOF if no assignments could be made.

Related topics:
fgets
fprintf
fputs
fread
fwrite
scanf
sscanf

fseek
Syntax:

      #include <stdio.h>
      int fseek( FILE *stream, long offset, int origin );

The function fseek() sets the file position data for the given stream. The origin value should have one of the following values (defined in stdio.h):

Name | Explanation
--- | ---
SEEK_SET | Seek from the start of the file
SEEK_CUR | Seek from the current location
SEEK_END | Seek from the end of the file

fseek() returns zero upon success, non-zero on failure. You can use fseek() to move beyond a file, but not before the beginning. Using fseek() clears the EOF flag associated with that stream.

Related topics:
fgetpos
fopen
fsetpos
ftell
rewind

fsetpos
Syntax:

      #include <stdio.h>
      int fsetpos( FILE *stream, const fpos_t *position );

The fsetpos() function moves the file position indicator for the given stream to a location specified by the position object. fpos_t is defined in stdio.h. The return value for fsetpos() is zero upon success, non-zero on failure.

Related topics:
fgetpos
fseek
ftell

ftell
Syntax:

      #include <stdio.h>
      long ftell( FILE *stream );

The ftell() function returns the current file position for stream, or -1 if an error occurs.

Related topics:
fgetpos
fseek
fsetpos

fwrite
Syntax:

      #include <stdio.h>
      int fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

The fwrite() function writes, from the array buffer, count objects of size size to stream. The return value is the number of objects written.

Related topics:
fflush
fgetc
fopen
fputc
fread
fscanf
getc

getc
Syntax:

      #include <stdio.h>
      int getc( FILE *stream );

The getc() function returns the next character from stream, or EOF if the end of file is reached. getc() is identical to fgetc(). For example:

       char ch;
       FILE *input = fopen( "stuff", "r" );

       ch = getc( input );
       while( ch != EOF ) {
         printf( "%c", ch );
         ch = getc( input );
       }

Related topics:
feof
fflush
fgetc
fopen
fputc
fread
fwrite
putc
ungetc

getchar
Syntax:

      #include <stdio.h>
      int getchar( void );

The getchar() function returns the next character from STDIN, or EOF if the end of file is reached.

Related topics:
fgetc
fopen
fputc
putc

gets
Syntax:

      #include <stdio.h>
      char *gets( char *str );

The gets() function reads characters from STDIN and loads them into str, until a newline or EOF is reached. The newline character is translated into a null termination. The return value of gets() is the read-in string, or NULL if there is an error.

Related topics:
fgetc
fgets
fputs
puts

perror
Syntax:

      #include <stdio.h>
      void perror( const char *str );

The perror() function prints str and an implementation-defined error message corresponding to the global variable errno.

Related topics:
clearerr
feof
ferror

printf
Syntax:

      #include <stdio.h>
      int printf( const char *format, ... );

The printf() function prints output to STDOUT, according to format and other arguments passed to printf(). The string format consists of two types of items - characters that will be printed to the screen, and format commands that define how the other arguments to printf() are displayed. Basically, you specify a format string that has text in it, as well as "special" characters that map to the other arguments of printf(). For example, this code

       char name[20] = "Bob";
       int age = 21;
       printf( "Hello %s, you are %d years old\n", name, age );

displays the following output:

       Hello Bob, you are 21 years old

The %s means, "insert the first argument, a string, right here." The %d indicates that the second argument (an integer) should be placed there. There are different %-codes for different variable types, as well as options to limit the length of the variables and whatnot.

Code | Format
--- | ---
%c | character
%d | signed integers
%i | signed integers
%e | scientific notation, with a lowercase "e"
%E | scientific notation, with a uppercase "E"
%f | floating point
%g | use %e or %f, whichever is shorter
%G | use %E or %f, whichever is shorter
%o | octal
%s | a string of characters
%u | unsigned integer
%x | unsigned hexadecimal, with lowercase letters
%X | unsigned hexadecimal, with uppercase letters
%p | a pointer
%n | the argument shall be a pointer to an integer into which is placed the number of characters written so far
%% | a '%' sign

An integer placed between a % sign and the format command acts as a minimum field width specifier, and pads the output with spaces or zeros to make it long enough. If you want to pad with zeros, place a zero before the minimum field width specifier:

       %012d

You can also include a precision modifier, in the form of a .N where N is some number, before the format command:

       %012.4d

The precision modifier has different meanings depending on the format command being used:

- With %e, %E, and %f, the precision modifier lets you specify the number of decimal places desired. For example, %12.6f will display a floating number at least 12 digits wide, with six decimal places.
- With %g and %G, the precision modifier determines the maximum number of significant digits displayed.
- With %s, the precision modifer simply acts as a maximumfield length, to complement the minimum field length that precedes the period.

All of printf()'s output is right-justified, unless you place a minus sign right after the % sign. For example,

       %-12.4f

will display a floating point number with a minimum of 12 characters, 4 decimal places, and left justified. You may modify the %d, %i, %o, %u, and %x type specifiers with the letter l and the letter h to specify long and short data types (e.g. %hd means a short integer). The %e, %f, and %g type specifiers can have the letter l before them to indicate that a double follows. The %g, %f, and %e type specifiers can be preceded with the character '#' to ensure that the decimal point will be present, even if there are no decimal digits. The use of the '#' character with the %x type specifier indicates that the hexidecimal number should be printed with the '0x' prefix. The use of the '#' character with the %o type specifier indicates that the octal value should be displayed with a 0 prefix.

You can also include constant escape sequences in the output string.

The return value of printf() is the number of characters printed, or a negative number if an error occurred.

Related topics:
fprintf
puts
scanf
sprintf

putc
Syntax:

      #include <stdio.h>
      int putc( int ch, FILE *stream );

The putc() function writes the character ch to stream. The return value is the character written, or EOF if there is an error. For example:

       char ch;
       FILE *input, *output;
       input = fopen( "tmp.c", "r" );
       output = fopen( "tmpCopy.c", "w" );
       ch = getc( input );
       while( ch != EOF ) {
         putc( ch, output );
         ch = getc( input );
       }
       fclose( input );
       fclose( output );

generates a copy of the file tmp.c called tmpCopy.c.

Related topics:
feof
fflush
fgetc
fputc
getc
getchar
putchar
puts

putchar
Syntax:

      #include <stdio.h>
      int putchar( int ch );

The putchar() function writes ch to STDOUT. The code

       putchar( ch );

is the same as

       putc( ch, STDOUT );

The return value of putchar() is the written character, or EOF if there is an error.

Related topics:
putc

puts
Syntax:

      #include <stdio.h>
      int puts( char *str );

The function puts() writes str to STDOUT. puts() returns non-negative on success, or EOF on failure.

Related topics:
fputs
gets
printf
putc

remove
Syntax:

      #include <stdio.h>
      int remove( const char *fname );

The remove() function erases the file specified by fname. The return value of remove() is zero upon success, and non-zero if there is an error.

Related topics:
rename

rename
Syntax:

      #include <stdio.h>
      int rename( const char *oldfname, const char *newfname );

The function rename() changes the name of the file oldfname to newfname. The return value of rename() is zero upon success, non-zero on error.

Related topics:
remove

rewind
Syntax:

      #include <stdio.h>
      void rewind( FILE *stream );

The function rewind() moves the file position indicator to the beginning of the specified stream, also clearing the error and EOF flags associated with that stream.

Related topics:
fseek

scanf
Syntax:

      #include <stdio.h>
      int scanf( const char *format, ... );

The scanf() function reads input from STDIN, according to the given format, and stores the data in the other arguments. It works a lot like printf(). The format string consists of control characters, whitespace characters, and non-whitespace characters. The control characters are preceded by a % sign, and are as follows:

Control Character | Explanation
--- | ---
%c | a single character
%d | a decimal integer
%i | an integer
%e, %f, %g | a floating-point number
%o | an octal number
%s | a string
%x | a hexadecimal number
%p | a pointer
%n | an integer equal to the number of characters read so far
%u | an unsigned integer
%[] | a set of characters
%% a percent sign

scanf() reads the input, matching the characters from format. When a control character is read, it puts the value in the next variable. Whitespace (tabs, spaces, etc) are skipped. Non-whitespace characters are matched to the input, then discarded. If a number comes between the % sign and the control character, then only that many characters will be converted into the variable. If scanf() encounters a set of characters, denoted by the %[] control character, then any characters found within the brackets are read into the variable. The return value of scanf() is the number of variables that were successfully assigned values, or EOF if there is an error.

Example code:

This code snippet repeatedly uses scanf() to read integers and floats from the user. Note that the variable arguments to scanf() are passed in by reference, as denoted by the ampersand (&) preceding each variable:

     int i;
     float f;

     while( 1 ) {
       printf( "Enter an integer: " );
       scanf( "%d", &i );

       printf( "Enter a float: " );
       scanf( "%f", &f );

       printf( "You entered %d and then %f\n", i, f );
     }

Related topics:
fgets
fscanf
printf
sscanf

setbuf
Syntax:

      #include <stdio.h>
      void setbuf( FILE *stream, char *buffer );

The setbuf() function sets stream to use buffer, or, if buffer is null, turns off buffering. If a non-standard buffer size is used, it should be BUFSIZ characters long.

Related topics:
fclose
fopen
setvbuf

setvbuf
Syntax:

      #include <stdio.h>
      int setvbuf( FILE *stream, char *buffer, int mode, size_t size );

The function setvbuf() sets the buffer for stream to be buffer, with a size of size. mode can be:

- _IOFBF, which indicates full buffering
- _IOLBF, which means line buffering
- _IONBF, which means no buffering

Related topics:
setbuf

sprintf
Syntax:

      #include <stdio.h>
      int sprintf( char *buffer, const char *format, ... );

The sprintf() function is just like printf(), except that the output is sent to buffer. The return value is the number of characters written. For example:

       char string[50];
       int file_number = 0;

       sprintf( string, "file.%d", file_number );
       file_number++;
       output_file = fopen( string, "w" );

Note that sprintf() does the opposite of a function like (Standard C String and Character) atoi() -- where (Standard C String and Character) atoi() converts a string into a number, sprintf() can be used to convert a number into a string.

For example, the following code uses sprintf() to convert an integer into a string of characters:

       char result[100];
       int num=24;
       sprintf( result, "%d", num );

Related topics:
(Standard C String and Character) atof
(Standard C String and Character) atoi
(Standard C String and Character) atol
fprintf
printf

sscanf
Syntax:

      #include <stdio.h>
      int sscanf( const char *buffer, const char *format, ... );

The function sscanf() is just like scanf(), except that the input is read from buffer.

Related topics:
fscanf
scanf

tmpfile
Syntax:

      #include <stdio.h>
      FILE *tmpfile( void );

The function tempfile() opens a temporary file with an unique filename and returns a pointer to that file. If there is an error, null is returned.

Related topics:
tmpnam

tmpnam
Syntax:

      #include <stdio.h>
      char *tmpnam( char *name );

The tmpnam() function creates an unique filename and stores it in name. tmpnam() can be called up to TMP_MAX times.

Related topics:
tmpfile

ungetc
Syntax:

      #include <stdio.h>
      int ungetc( int ch, FILE *stream );

The function ungetc() puts the character ch back in stream.

Related topics:
getc
(C++ I/O) putback

vprintf, vfprintf, and vsprintf
Syntax:

      #include <stdarg.h>
      #include <stdio.h>
      int vprintf( char *format, va_list arg_ptr );
      int vfprintf( FILE *stream, const char *format, va_list arg_ptr );
      int vsprintf( char *buffer, char *format, va_list arg_ptr );

These functions are very much like printf(), fprintf(), and sprintf(). The difference is that the argument list is a pointer to a list of arguments. va_list is defined in stdarg.h, and is also used by (Other Standard C Functions) va_arg(). For example:

       void error( char *fmt, ... ) {
         va_list args;
         va_start( args, fmt );
         fprintf( stderr, "Error: " );
         vfprintf( stderr, fmt, args );
         fprintf( stderr, "\n" );
         va_end( args );
         exit( 1 );
       }