Jump to content

stdio.h

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Full Decent (talk | contribs) at 01:25, 1 July 2008 (link to puts_(C)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations. The functionality descends from a "portable I/O package" written by Mike Lesk at Bell Labs in the early 1970s.[1] C++, for compatibility reasons, includes stdio.h as well as an essentially equivalent header, cstdio.

Functions declared in stdio.h are extremely popular, since as a part of the C standard library, they are guaranteed to work on any platform that supports C. Applications on a particular platform might, however, have reasons to use the platform's I/O routines, rather than the stdio.h routines.

Example usage

All functions in C (and its many derivatives) are declared in header files. Thus, programmers have to include the stdio.h header in the source code in order to use the functions declared in it.

#include <stdio.h>
 
int main(void)
{
    int ch;
    while ((ch = getchar()) != EOF)
        putchar(ch);
    putchar('\n');
    return 0;
}

The above program reads all input from standard input and echoes it to standard output, byte by byte, adding a newline at the end of its output.

Member functions

Functions declared in stdio.h can generally be divided into two categories: the functions for file manipulation and the functions for input-output manipulation.

Name Notes
File manipulation functions
fclose closes a file associated with the FILE * value passed to it
fopen, freopen, fdopen opens a file for certain types of reading or writing
remove removes a file (deletes it)
rename renames a file
rewind acts as if fseek(stream, 0L, SEEK_SET) was called for the stream passed, and then its error indicator cleared
tmpfile creates and open a temporary file, which is deleted when closed with fclose()
Input-output manipulation functions
clearerr clears end-of-file and error indicators for a given stream
feof checks whether an end-of-file indicator has been set for a given stream
ferror checks whether an error indicator has been set for a given stream
fflush forces any pending buffered output to be written to the file associated with a given stream
fgetpos stores the file position indicator of the stream associated by its first argument (a FILE *) to its second argument (a fpos_t *)
fgetc returns one character from a file
fgets gets a string from the file (ending at newline or end-of-file)
fputc writes one character to a file
fputs writes a string to a file
ftell returns a file-position indicator which can then be passed to fseek
fseek seeks through a file
fsetpos sets the file position indicator of a stream associated by its first argument (a FILE *) as stored in its second argument (a fpos_t *)
fread reads data from a file
fwrite writes data to a file
getc reads and returns a character from a given stream and advances the file position indicator; it is allowed to be a macro with the same effects as fgetc, except that it may evaluate the stream more than once
getchar has the same effects as getc(stdin)
gets reads characters from stdin until a newline is encountered and stores them in its only argument
printf, vprintf used to print to the standard output stream
fprintf, vfprintf used to print to a file
sprintf, snprintf, vsprintf used to print to a char array (C string)
perror writes an error message to stderr
putc writes and returns a character to a stream and advances the file position indicator for it; it is allowed to be a macro with the same effects as fputc, except that it may evaluate the stream more than once
putchar, fputchar has the same effects as putc(stdout)
scanf, vscanf used to input from the standard input stream
fscanf, vfscanf used to input from a file
sscanf, vsscanf used to input from a char array (e.g., a C string)
setbuf
setvbuf sets the buffering mode for a given stream
tmpnam creates a temporary filename
ungetc pushes a character back onto a stream
puts_(C) outputs a character string to stdout

Member constants

Constants defined in the stdio.h header include:

Name Notes
EOF a negative integer of type int used to indicate end-of-file conditions
BUFSIZ an integer which is the size of the buffer used by the setbuf() function
FILENAME_MAX the size of a char array which is large enough to store the name of any file that can be opened
FOPEN_MAX the number of files that may be open simultaneously; will be at least 8
_IOFBF an abbreviation for "input/output fully buffered"; it is an integer which may be passed to the setvbuf() function to request block buffered input and output for an open stream
_IOLBF an abbreviation for "input/output line buffered"; it is an integer which may be passed to the setvbuf() function to request line buffered input and output for an open stream
_IONBF an abbreviation for "input/output not buffered"; it is an integer which may be passed to the setvbuf() function to request unbuffered input and output for an open stream
L_tmpnam the size of a char array which is large enough to store a temporary filename generated by the tmpnam() function
NULL a macro expanding to the null pointer constant; that is, a constant representing a pointer value which is guaranteed not to be a valid address of an object in memory
SEEK_CUR an integer which may be passed to the fseek() function to request positioning relative to the current file position
SEEK_END an integer which may be passed to the fseek() function to request positioning relative to the end of the file
SEEK_SET an integer which may be passed to the fseek() function to request positioning relative to the beginning of the file
TMP_MAX the maximum number of unique filenames generable by the tmpnam() function; will be at least 25

Member variables

Variables defined in the stdio.h header include:

Name Notes
stdin a pointer to FILE which refers to the standard input stream, usually a keyboard.
stdout a pointer to FILE which refers to the standard output stream, usually a display terminal.
stderr a pointer to FILE which refers to the standard error stream, often a display terminal.

Member types

Data types defined in the stdio.h header include:

  • FILE - a structure containing the information about a file or text stream needed to perform input or output operations on it, including:
    • a file descriptor
    • the current stream position
    • an end-of-file indicator
    • an error indicator
    • a pointer to the stream's buffer, if applicable
  • fpos_t - a non-array type capable of uniquely identifying the position of every byte in a file.
  • size_t - an unsigned integer type which is the type of the result of the sizeof operator.

See also

References

  1. ^ Kernighan, Brian (1984). The UNIX Programming Environment. Englewood Cliffs: Prentice Hall. pp. pg. 200. {{cite book}}: |pages= has extra text (help); Unknown parameter |coauthors= ignored (|author= suggested) (help)