Everything
7.4.11 <stdio.h>

Performs processing relating to input/output of stream input/output file.
The following constants (macros) are all implementation-defined.

Type

Definition Name

Description

Constant

(macro)

FILE

Indicates a structure type that stores various control information including a pointer to the buffer, an error indicator, and an end-of-file indicator, which are required for stream input/output processing.

_IOFBF

Indicates full buffering of input/output as the buffer area usage method.

_IOLBF

Indicates line buffering of input/output as the buffer area usage method.

_IONBF

Indicates non-buffering of input/output as the buffer area usage method.

BUFSIZ

Indicates the buffer size required for input/output processing.

EOF

Indicates end-of-file, that is, no more input from a file.

L_tmpnam*

Indicates the size of an array large enough to store a string of a temporary file name generated by the tmpnam function.

SEEK_CUR

Indicates a shift of the current file read/write position to an offset from the current position.

SEEK_END

Indicates a shift of the current file read/write position to an offset from the end-of-file position.

SEEK_SET

Indicates a shift of the current file read/write position to an offset from the beginning of the file.

SYS_OPEN*

Indicates the number of files for which simultaneous opening is guaranteed by the implementation.

TMP_MAX*

Indicates the maximum number of unique file names that shall be generated by the tmpnam function.

stderr

Indicates the file pointer to the standard error file.

stdin

Indicates the file pointer to the standard input file.

stdout

Indicates the file pointer to the standard output file.

Function

fclose

Closes a stream input/output file.

fflush

Outputs stream input/output file buffer contents to the file.

fopen

Opens a stream input/output file under the specified file name.

freopen

Closes a currently open stream input/output file and reopens a new file under the specified file name.

setbuf

Defines and sets a stream input/output buffer area on the user program side.

setvbuf

Defines and sets a stream input/output buffer area on the user program side.

fprintf

Outputs data to a stream input/output file according to a format.

vfprintf

Outputs a variable parameter list to the specified stream input/output file according to a format.

printf

Converts data according to a format and outputs it to the standard output file (stdout).

vprintf

Outputs a variable parameter list to the standard output file (stdout) according to a format.

sprintf

Converts data according to a format and outputs it to the specified area.

sscanf

Inputs data from the specified storage area and converts it according to a format.

snprintf <-lang=c99>

Converts data according to a format and writes it to the specified array.

vsnprintf <-lang=c99>

Equivalent to snprintf with the variable argument list replaced by va_list.

vfscanf <-lang=c99>

Equivalent to fscanf with the variable argument list replaced by va_list.

vscanf <-lang=c99>

Equivalent to scanf with the variable argument list replaced by va_list.

vsscanf <-lang=c99>

Equivalent to sscanf with the variable argument list replaced by va_list.

fscanf

Inputs data from a stream input/output file and converts it according to a format.

scanf

Inputs data from the standard input file (stdin) and converts it according to a format.

vsprintf

Outputs a variable parameter list to the specified area according to a format.

fgetc

Inputs one character from a stream input/output file.

fgets

Inputs a string from a stream input/output file.

fputc

Outputs one character to a stream input/output file.

fputs

Outputs a string to a stream input/output file.

getc

(macro) Inputs one character from a stream input/output file.

getchar

(macro) Inputs one character from the standard input file.

gets

Inputs a string from the standard input file.

putc

(macro) Outputs one character to a stream input/output file.

putchar

(macro) Outputs one character to the standard output file.

puts

Outputs a string to the standard output file.

ungetc

Returns one character to a stream input/output file.

Function

fread

Inputs data from a stream input/output file to the specified storage area.

fwrite

Outputs data from a storage area to a stream input/output file.

fseek

Shifts the current read/write position in a stream input/output file.

ftell

Obtains the current read/write position in a stream input/output file.

rewind

Shifts the current read/write position in a stream input/output file to the beginning of the file.

clearerr

Clears the error state of a stream input/output file.

feof

Tests for the end of a stream input/output file.

ferror

Tests for stream input/output file error state.

perror

Outputs an error message corresponding to the error number to the standard error file (stderr).

Type

fpos_t

Indicates a type that can specify any position in a file.

Constant (macro)

FOPEN_MAX

Indicates the maximum number of files that can be opened simultaneously.

FILENAME_MAX

Indicates the maximum length of a file name that can be held.

Note

* These macros are not defined in this implementation.

 

Implementation-Defined Specifications

Item

Compiler Specifications

Whether the last line of the input text requires a new-line character indicating the end

Not specified. Depends on the low-level interface routine specifications.

Whether the space characters written immediately before the new-line character are read

Number of null characters added to data written in the binary file

Initial value of file position indicator in the append mode

Whether file data is lost after output to a text file

File buffering specifications

Whether a file with file length 0 exists

File name configuration rule

Whether the same file is opened simultaneously

Output data representation of the %p format conversion in the fprintf function

Hexadecimal representation.

Input data representation of the %p format conversion in the fscanf function.

The meaning of conversion specifier '−' in the fscanf function

Hexadecimal representation.

 

If '−' is not the first or last character or '−' does not follow '^', the range from the previous character to the following character is indicated.

Value of errno specified by the fgetpos or ftell function

The fgetpos function is not supported.
The errno value for the ftell function is not specified. It depends on the low-level interface routine specifications.

Output format of messages generated by the perror function

See (a) below for the output message format.

 

(a) The output format of perror function is

<string>:<error message for the error number specified in error>

 

(b) Table 7.8 shows the format when displaying the floating-point infinity and not-a-number in printf and fprintf functions.

Table 7.8

Display Format of Infinity and Not-a-Number

Value

Display Format

Positive infinity

++++++

Negative infinity

------

Not-a-number

******

 

An example of a program that performs a series of input/output processing operations for a stream input/output file is shown in the following.

[Format]

   1   #include <stdio.h>
   2
   3  void main( )
   4  {
   5      int c;
   6      FILE *ifp, *ofp;
   7
   8      if ((ifp=fopen("INPUT.DAT","r"))==NULL){
   9          fprintf(stderr,"cannot open input file\n");
   10         exit(1);
   11      }
   12      if ((ofp=fopen("OUTPUT.DAT","w"))==NULL){
   13          fprintf(stderr,"cannot open output file\n");
   14          exit(1);
   15      }
   16      while ((c=getc(ifp))!=EOF)
   17         putc(c, ofp);
   18      fclose(ifp);
   19      fclose(ofp);
   20  }

Explanation:

This program copies the contents of file INPUT.DAT to file OUTPUT.DAT.

Input file INPUT.DAT is opened by the fopen function in line 8, and output file OUTPUT.DAT is opened by the fopen function in line 12. If opening fails, NULL is returned as the return value of the fopen function, an error message is output, and the program is terminated.

If the fopen function ends normally, the pointer to the data (FILE type) that stores information on the opened files is returned; these are set in variables ifp and ofp.

After successful opening, input/output is performed using these FILE type data.

When file processing ends, the files are closed with the fclose function.