C Standard I/O Library

Source: Internet
Author: User

In this post, we will introduce the following library functions in the I/O library.

fopen、fclosefread、fwritefflush-fseek-fgetc 、getc 、getcharfputc 、putc 、putcharfgets 、getsprintf 、fprintf 、sprintfscanf 、fscanf 、sscanf

The first is the fopen function. The fopen function prototype is as follows:

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

Fopen open the file specified by the filename parameter and associate it with a file stream. The mode parameter specifies the file opening mode.
R: open the file in read-only mode. The file must exist.
R + can open a file in read/write mode. The file must exist.
RB + read/write opens a binary file and allows reading data.
RW + read/write opens a text file, allowing reading and writing.
W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created.
W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created.
A. Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (EOF reserved)
A + opens readable and writable files by appending them. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (The original EOF is not retained)
WB only opens or creates a new binary file; only data can be written.
WB + enables read/write operations or creates a binary file, allowing read and write operations.
Wt + read/write opens or creates a text file; read/write is allowed.
At + read/write opens a text file, allowing you to read or append data at the end of the text.
Open a binary file through AB + read/write, and allow you to read or append data to the end of the file.
If fopen is successful, a non-empty file * pointer is returned. If fopen fails, a null value is returned. The null value is defined in stdio. h.

Fread function, the function prototype is as follows:

#include<stdio.h>size_t fread(void *ptr,size_t size,size_t nitems,FILE *stream);

Fread reads the data buffer directed by PTR from the file stream. Size specifies the length of each data record. The counter nitems shows the number of records to be transferred.

The returned value is the number of records successfully read into the data buffer. When the end of the file is reached, its return value may be less than nitems, or even 0.

Fwrite function. The function prototype is as follows:

#include<stdio.h>size_t fwrite(const void *ptr,size_t size,size_t nitems ,FILE *stream);

We do not recommend that you use fread and fwrite for structured data because different computer architectures may not be portable.

Fclose function. The function prototype is as follows:

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

Used to close the file stream specified by stream.

Fflush function. The function prototype is as follows:

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

The function is to immediately write the file out of all unwritten data. You can use this function to ensure that an interactive prompt is sent to the terminal before trying to read a user.

Fflush () is called implicitly by default before fclose is called ();

Fseek function. The function prototype is as follows:

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

It is used to call the corresponding file flow function with the lseek system. It specifies a location in the file stream for the next read/write operation.

Fgetc GETC getchar function. The function prototype is as follows:

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

The fgetc function extracts the next byte from the file stream and returns it as a character.

GETC serves the same purpose as fgetc, but may be implemented as a macro.

Getchar is equivalent to GETC (stdin), which reads the next character from the standard input.

Fputc putc putchar function. The function prototype is as follows:

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

The fputc function writes a character to the output file stream.

The putc function works the same as fputc, but it may be implemented as a macro.

Putchar is equivalent to writing a single character to the standard output.

Fgets gets function. The function prototype is as follows:

char *fgets(char *s, int n,FILE *stream);char *gets(char *s);

Fgets reads a string from the stream file stream and points to this string with S. In the following situation, a line break has been transmitted with n-1 characters or reached the end of the file.

It will also pass the line break to the string and add a '\ 0' at the end. Therefore, only n-1 characters can be transmitted at a time.

Gets reads a string from the standard input. It adds a null value to the end of the accepted string.

Format input and output:

Printf () sprintf () fprintf () function. The function prototype is as follows:

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

Printf () is a formatting output function, which is generally used to output data to a standard output device in the specified format.

The sprintf () string formatting command is mainly used to write formatted data into a string.

The fprintf () function sends information (parameters) to the file specified by stream (Stream) based on the specified format (Format). fprintf () can only work like printf.

The return value of fprintf () is the number of output characters. If an error occurs, a negative value is returned.

 

Format:

% Indicates the percentage, which is not converted.

The % C integer is converted to the corresponding ASCII character.

% D integer to decimal place.

% F times the precision number to a floating point number.

% O integer to octal.

% S integer to string.

% X integer to lowercase hexadecimal.

% X integer to uppercase hexadecimal.

Scanf () fscanf () sscanf () function. The function prototype is as follows:

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

Sscanf ()-read data that matches the specified format from a string.

Scanf () reads the specified input from the standard input

Fscanf () reads a specified input from the file

Format description

Conversion character (followed by %)

A read floating point value (c99 only)

A read floating point value (c99 only)

C read single character

D. Read the decimal integer.

I-read decimal, octal, and hexadecimal Integers

E read floating point number

E read floating point number

F Read floating point number

F Read floating point number (c99 only)

G read floating point number

G read floating point number

O eight bytes read

S read string

X-read hexadecimal number

X-read hexadecimal number

P read pointer Value

N The number of equivalent characters that have been read so far

Unsigned decimal integer read by u

[] Character Set Merging

% Read % (percent)

Additional format description: Comment table modifier description

L/L length modifier input "long" Data

H-length modifier input "short" Data

W integer constant specifies the width of input data

* Indicates that this input item is not assigned to the corresponding variable after reading.

 

% Indicates the percentage, which is not converted.

The % C integer is converted to the corresponding ASCII character.

% D integer to decimal place.

% F times the precision number to a floating point number.

% O integer to octal.

% S integer to string.

% X integer to lowercase hexadecimal.

% X integer to uppercase hexadecimal.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.