Tag: Io
It is independent of the operating system and can only operate regular files with a buffer.
Regular files: ASCII files (text files) and binary files
Buffer: reduces the number of user and kernel switches and reduces system overhead. The buffer zone is opened up only when files are operated. When the process ends, fclose () is automatically called to Close opened files and refresh the buffer zone.
Full Buffer: 4 K bytes, used for standard file operations, when the buffer is full to the kernel.
Row Buffer: 1 K bytes, used for standard input and output. When the buffer encounters '\ n', it is sent to the kernel.
No buffer: 0 bytes, used for standard errors. Data is directly sent to the kernel without passing through the buffer.
There is a high-speed cache in the kernel, which is regularly refreshed to the hard disk.
Description file descriptor the macro file pointer corresponding to the int file descriptor *
Standard input 0 stdin_fileno stdin
Standard output 1 stdout_fileno stdout
Standard Error 2 stderr_fileno stderr
Int fflush (File * FP); used to refresh the data in the buffer zone to the kernel in real time.
File * fopen (const char * path, const char * mode );
File * fdopen (int fd, const char * mode); // contact stream and file descriptor
File * freopen (const char * path, const char * mode, file * stream); // used for redirection
Int fclose (File * stream );
When a city ends normally (the exit function is called directly or returned from the main function), the system automatically calls the fclose () function to refresh all the buffers, all open standard I/O streams are disabled.
To meet programming specifications, you must explicitly call fclose () after using a file ().
Mode parameter:
R or RB: open a read-only file, which must exist.
R + or RB + to open a readable file. The file must exist.
W or WB open write-only files, and empty if the file exists. If the file does not exist, the file is created.
W + or WB + open a readable/writable file. If the file exists, it is cleared. If the file does not exist, it is created.
A or AB opens the write-only file as an attachment. If the file does not exist, it is created. If the file exists, the written data is appended to the end of the file, that is, the original content is retained.
A or AB + opens a read/write file as an attachment. If the file does not exist, it is created. If the file exists, the written data is appended to the end of the file, that is, the original content is retained.
B Indicates opening the file in binary mode.
Differences between binary and text Modes
1. in windows, in text mode, "\ r \ n" indicates a line break. If you open a file in text mode and use functions such as fputs to write the line break "\ n", the function automatically adds "\ r" to "\ n ". That is, "\ r \ n" is actually written to the file ".
2. in Unix/Linux-like text mode, "\ n" indicates a line break. Therefore, in Linux, there is no difference between the text mode and the binary mode.
I/O for each character: If the stream is buffered, the standard I/O function processes all the buffers.
Int fgetc (File * stream); EOF is returned if the end of the file or an error occurs.
While (! Feof (FP )&&! Ferror (FP ))
{
Ch = fgetc (FP );
If (CH = EOF) break;
Printf ("% C", CH );
}
Int fputc (int c, file * stream );
Int GETC (File * stream );
Int putc (File * stream );
The implementation is macro, which reads or writes from the stream.
Int getchar (void); read stdin from the terminal
Int putchar (int c); output to terminal stdout
Implementation is macro
Check file error functions
Int feof (File * stream );
Int ferror (File * stream );
Int clearerr (File * stream );
I/O of each row: each row ends with '\ N'
Char * fgets (char * s, int size, file * stream); returns NULL if the end of the file is located or if an error occurs.
Fgets can only read one row (up to 1024 bytes in a row) from the file at a time. In a text file, each row ends with '\ n', and fgets will automatically read the last line break, therefore, '\ n' is not required for printing '.
When fgets is used to read n bytes of data, only n-1 characters are returned, and the last one is '\ 0 '.
While (fgets (STR, 100, FP1 )! = NULL)
{
Fputs (STR, fp2 );
}
Int fputs (const char * s, file * stream );
Char * gets (char * s); int puts (const char * s );
Direct I/O: used to read and write blocks or struct
Size_t fread (void * PTR, size_t size, size_t nmemb, file * stream );
Read the size * nmemb byte from the stream to the address space starting with PTR.
Size_t fwrite (const void * PTR, size_t size, size_t nmemb, file * stream );
Locate the stream:
Int fseek (File * stream, long offset, int whence); used to set the File Location of the stream. Offset is a positive backward offset. It is a negative beginning offset. The whence parameter is seek_set (beginning with)/seek_cur (current)/seek_end (end ).
Long ftell (File * stream); get the current file location.
Void rewind (File * stream); used to set the File Location of the stream to start with the file.
Standard I/O (C library function)