3.1 the buffer

For standard IO libraries, which operate around streams, ASCII characters are represented by a single byte, and international characters, which are typically represented by multiple characters, you can use functions to change streams to be wide oriented

Int fwide(FILE* fp,int mode) mode<0 byte direction mode>0 Wide direction mode=0 This parameter is not set, but returns a value indicating the direction of the streamCopy the code

After opening a stream, a pointer to the FILE object is returned, usually including the FILE descriptor, a pointer to the stream buffer, the length of the buffer, the number of characters in the current buffer, and an error flag.

Three standard IO streams, stdin, STdout and stderr

Standard IO provides the following three buffers

(1) Full buffering

Do not operate until the buffer is filled

(2) Line buffering

Operates when a newline character is encountered, as well as whenever the buffer is filled

(3) No buffer

Standard IO libraries do not buffer characters

Note:

Standard error is not buffered

If a stream points to a terminal device, it is row buffering; Otherwise it’s all fully buffered

At any time, a stream can be forced to flush

Int fflush(FILE* fp) returns 0 on success, -1 otherwiseCopy the code

3.2 flow operation

Write operations are as follows:

​ []->fp

Read operations are as follows:

​ fp->[]

Open a standard IO stream using the following function

// Return FILE pointer on success, otherwise NULL FILE* fopen(const char* pathname,const char* type) type: B means binary r read w write, start from scratch a write, add r+ read/write open W + read/write open, but if there is no file will create a file, and the file truncation is 0 a+ file tail open, every write will write to the end of the file! FILE* freopen(const char* pathname,const char* type,FILE* fp) Opens a specified FILE on a specified stream FILE* fdopen(int fd,const char* type) Opens a file stream on a file descriptor, noting that this method does not truncate the file because the descriptor is already openCopy the code

To close the file, do as follows

Int fclose(FILE* fp) on success, returns 0, otherwise, returns EOF(-1) flush the output data in the buffer before the FILE is closed, and any input data in the buffer is discarded. When a process terminates normally, all standard IO streams with unwritten buffered data are flushed and all open standard IO streams are closedCopy the code

3.2.1 Character Operations

Read a character

Int geTC (FILE* fp) is the fastest, can be used as a macro! Int fgeTC (FILE* fp) int getChar (void) Reads from the FILE input streamCopy the code

Because of an error or the end of the file will return -1, you need to determine the error

Int ferror(FILE* fp) int ferof (FILE* fp) int ferof (FILE* fp) Void clearErr (FILE* fp) int ungeTC (int c,FILE* fp) ungeTC (int c,FILE* fp) Another one at the end of the file will make the EOF logo clear!Copy the code

To print a character:

EOF int putc(int c,FILE* fp) similarly, the macro int fputc(int c,FILE* fp) int putchar(int c) output to the output streamCopy the code

3.2.2 line flow operations

Char * fgets(char* buf,int n,FILE* fp) returns NULL char* fgets(char* buf,int n,FILE* fp) Char * gets(char* buf) : char* gets(char* buf) : char* gets(buf) : char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf: char* buf Otherwise, returning EOF int fputs(const char* STR,FILE* fp) does not output the line, even if it is read to \n. The NULL ending does not write int puts(const char* STR) writes a null-terminated string to the standard output. It does not end at \n, but writes another \n to the standard output each time it doesCopy the code

3.2.3 Binary I/O

// Returns the number of objects read or written
size_t fread(void* ptr,size_t size,size_tNobj,FILE* fp) size: the size of each array element, i.esizeof(ptr[0]) nobj: indicates the number of elements to be operated, i.esizeof(ptr)  // If it is an arrayIf the return value is less than nobj, EOF or an error may occursize_t fwrite(const void* ptr,size_t size,size_tNobj,FILE* fp) if the return value is smaller than nobj, an error must occurCopy the code

3.2.4 positioning flow

Int fseek(FILE* fp,long offset,int whence) long ftell(FILE* fp,long offset,int whence) long ftell(FILE* fp,long offset,int whence) And because the buffer may not be exactly the same file offset: <0 forward >0 back whence: SEEK_SET (0) Start position SEEK_CUR (1) Current position SEEK_END (2) End position void rewind(FILE* fp) Sets a stream to the start position of the FILECopy the code

3.2.5 Formatting I/OS

Write operations (formatting output) [formatting content]-> (FP) and other targets

Int printf(const char* format,...) int fprintf(FILE* fp,const char* format,...) Int dprintf(int fd,const char* format,...) -int sprintf(char* buf,const char* format,...) This function has no bounds checking and adds a NULL to the end of the array, but does not include it in the return value. Otherwise -int snprintf(char* buf,size_t n,const char* format,...) Write but with boundary checkingCopy the code

Read operation (format input) (FP) etc ->[format content]

// Return the number of input items, otherwise return EOF (error or end of file) int scanf(const char* format,...) int fscanf(FILE* fp,const char* format,...) Int sscanf(const char* buf,const char* format...) Read formatted input from array to... In theCopy the code

3.2.6 memory stream

In Linux, memory can be manipulated as a file to make that file operate very quickly

// Returns a stream pointer on success, Otherwise, NULL FILE* fmemopen(void *buf,size_t size,const char*type) is returned. The caller provides a block of memory to this function using buf and size After opening this file, the related operations of the file are available to its stream!Copy the code