C Language: File-I/O Operation

C Language: File-I/O Operation

For the input stream associated with the terminal, if the last byte in the buffer has been read and it tries to continue reading, the terminal is requested to input, and the program control is transferred to the terminal.

Format input and output

Regarding format control documents or tutorials are available everywhere on the Internet, I will not repeat them, and will skip these content directly.

fprintf

int fprintf (FILE * restrict stream, const char * restrict format, ...) ; copy code

fprintf
The function writes characters into the stream, and the data is provided by the format string and the following parameters.

The function returns the number of characters actually written, or a negative number if an error occurs.

If more parameters are given than required by the format control, the extra parameters will be ignored. If there are fewer parameters than the format control requires, the behavior is undefined.

fprintf
Is a variable parameter function . For the parameter list
...
The variable parameter function at the end, or the function declared in the traditional way instead of the function prototype, there is a default parameter promotion when calling the function . All lengths less than
int
Are promoted to
int
,all
float
Have been promoted to
double
. When outputting an integer or floating-point number, the function passes the value instead of the address, and then after parameter promotion, there is no need to care about the length of the original type. This is why
float
with
double
Both use
%f
Format, while
char
,
short
Etc. can be used
%d
Format, without warning.

fscanf

int fscanf (FILE * restrict stream, const char * restrict format, ...) ; copy code

fscanf
Read characters from the stream, and write the converted data into the position pointed to by the following parameter according to the format control.

The function returns the number of successfully written parameters, which may be 0 or any positive integer. If the end of the file or an error is encountered directly before any characters are successfully read, EOF is returned .

If more parameters are given than required by the format control, the extra parameters will be ignored. If there are fewer parameters than the format control requires, the behavior is undefined.

The parameters passed after the format string should all be pointers, and their values will be treated as pointers when the function is processed. There is a parsing process for reading data from the stream. The parsing is carried out in sequence according to the flow direction. If the input content meets the format control, when the writing conditions are met, the characters are converted into the corresponding data write parameter to the position pointed to; if from a certain If the character does not conform to the format control at the beginning, the parsing will fail, the function will terminate, all the written data will be retained, and the unprocessed parameters will be ignored. The parsing failure will not generate an error, and the file position indicator will be placed at the character where the parsing failed.

fscanf
It is also a variable parameter function . But with
fprintf
different,
fscanf
Almost only accepts pointers, and the default parameter promotion has almost no effect on it. If you want to write data in a certain memory, you must know the data type and data length, so
fscanf
Strict distinction
float
with
double
,
char
,
short
,
int
And so can not be mixed.

printf

int printf ( const char * restrict format, ...) ; copy code

printf
The function is the same
fprintf
Function, but the default stream is stdout .

scanf

int scanf ( const char * restrict format, ...) ; copy code

scanf
The function is the same
fscanf
Function, but the default stream is stdin .

snprintf

int the snprintf ( char * the restrict S, size_t n-, const char * the restrict the format, ...) ; duplicated code

snprintf
The function is similar to
fprintf
Function, but the data is not written to the stream, but written to the character array.

s represents the array that accepts the output, and n represents the size of the array. If the output content is more than n-1 bytes, the excess content will be discarded.

snprintf
A null character will be added to the end of the data written to the array
'\0'
.

If s is a null pointer or n is 0, nothing will be written.

Because the array does not have a buffer, the written content will appear in the array immediately.

snprintf
The function returns the number of characters actually written (not including the null character at the end), and returns a negative number if an error occurs.

sprintf

int sprintf ( char * restrict s, const char * restrict format, ...) ; copy code

sprintf
The function is similar to
snprintf
Function, but there is no array size limit.

sscanf

int sscanf ( const char * the restrict S, const char * the restrict the format, ...) ; duplicated code

sscanf
The function is similar to
fscanf
Function, but instead of reading the stream, it reads the string.

sscanf
The function will put the null character at the end of the string
'\0'
Treat as the end of the file. Since the string is not a stream, there is no file location pointer or buffer, the result of reading the same string in the same way every time is the same.

The function returns the number of successfully written parameters. If a null character or error is encountered directly before any character is successfully read, EOF is returned .

Character input and output

fputc

int fputc ( int c, FILE *stream) ; copy the code

fputc
The function writes a byte to the stream. Note that the parameters are
int
Type, will be converted to
unsigned char
Types of.

The function returns what it actually wrote, and the return value type is also

int
. If an error occurs, EOF is returned .

putc

int putc ( int c, FILE *stream) ; copy the code

putc
The function is the same
fputc
Functions, but generally implemented as macros. So the parameters should not have side effects.

putchar

int putchar ( int c) ; copy the code

putchar
The function is the same
putc
Function, but the default stream is stdout .

fgetc

int fgetc (FILE *stream) ; copy code

fgetc
The function reads a byte from the stream, and then from
unsigned char
Type conversion to
int
Type and return.

The function returns the read content, if it encounters the end of the file or an error, it returns EOF .

getc

int getc (FILE *stream) ; copy code

getc
The function is the same
fgetc
Functions, but generally implemented as macros. So the parameters should not have side effects.

getchar

int getchar ( void ) ; copy code

getchar
The function is the same
getc
Function, but the default stream is stdin .

fgets

char * fgets ( char * restrict s, int n, FILE * restrict stream) ; Copy code

fgets
The function reads up to n-1 bytes from the stream and stores them in the array s . If you encounter a newline
'\n'
Or at the end of the file, stop reading (newline characters will be read and stored).
fgets
A null character will be added to the end of the data stored in the array
'\0'
.

The function returns the parameter s successfully . If the end of the file or an error is encountered directly before any characters are successfully read, a null pointer is returned.

fputs

int fputs ( const char * restrict s, FILE * restrict stream) ; Copy code

fputs
The function writes the content of the string s into the stream, and a null character is encountered
'\0'
It stops, and the null character will not be written.

The function returns a non-negative integer if it succeeds, and returns EOF if it fails .

puts

int puts ( const char *s) ; copy code

puts
The function is similar to
fputs
, But the default stream is stdout , and a newline character is output at the end
'\n'
.

Due to security issues,

gets
The function has been deleted in the C11 standard, and this article will not discuss it.

ungetc

int ungetc ( int c, FILE *stream) ; copy the code

ungetc
The function converts a character (by
int
Type conversion to
unsigned char
Type) In the reflow, if the read operation continues, the characters are read in the reverse order of the reflow order.

The function returns the character actually put back after successful execution, and returns EOF if it fails .

ungetc
The function behaves as if it writes in reverse and overwrites the data in the original stream. Each time it writes one byte before the current position and decrements the file position indicator. Therefore, the successful execution
ungetc
After the function, the file position indicator always points to the character just put back.

ungetc
The function does not actually put the characters in the stream, but in another temporary buffer. any
ungetc
The characters put in the function will not be saved to the file, and once called
fseek
,
fsetpos
,
rewind
Wait for the function that sets the file position pointer, all the ones that have not been read are caused by
ungetc
The characters returned by the function are discarded. In many implementations, write operations are also discarded
ungetc
The character put back by the function.

If the parameter c is EOF , the function fails and returns. If the function executes successfully, the EOF flag of the stream will be reset .

ungetc
The design and mechanism of the function are weird and should be used with caution.

Direct input and output

Direct input and output are mostly used for reading and writing binary files.

fread

size_t fread ( void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream) ; Copy code

fread
The function reads nmemb elements from the stream , each element is size bytes, and the read content is sequentially written to the memory area pointed to by ptr .

The function returns the number of elements successfully read, that is, a non-negative integer less than or equal to nmemb .

fread
Function reads at most
nmemb * size
Bytes. If there is not enough data in the stream, the reading stops at the end of the file. At this time, if the last element does not meet size bytes, this element is not counted in the return value. Because the data written to the memory pointed to by ptr has no definite ending character, in order to know how many bytes the function reads, size should be set to 1 .

fwrite

size_t fwrite ( const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream) ; Copy code

fwrite
The function writes nmemb elements to the stream , each element is size bytes, and the data written comes from the memory area pointed to by ptr .

The function returns the number of successfully written elements, that is, a non-negative integer less than or equal to nmemb .

fwrite
Function writes at most
nmemb * size
Bytes. If a write error is encountered, the function returns. At this time, if the last element does not meet size bytes, this element is not counted in the return value. The size is also recommended to be set to 1 .