1. Linux under All documents, therefore, the read-write operation of the file is indispensable; I/O programming for files includes primary file programming and standard file programming, the so-called standard I/O file programming is with "buffered I/O", while the primary file I/O programming is not buffered; all primary i/ The O function is for the "file descriptor", when we open a file, a "file descriptor" is returned, and the file descriptor is used for subsequent I/O operations, while the standard I/O libraries revolve around "streaming," when you open or create a file with a standard I/O library, Causes a stream to be combined with a file, and when a stream is opened, the standard I/O function fopen Returns a pointer to the file object.
2. The term "file" refers to an ordered set of related data, which is called a file, and the name of the dataset is called a file. Classification of documents: from the user's point of view, can be divided into ordinary files and equipment files two classes (ordinary files: refers to the storage of the equipment on the surface of some files, documents, pictures, etc. device files: such as monitors, printers, etc.) From the way the file is encoded: can be divided into text files and binary files (text files: Also known as ASCII files, which are stored on disk, each character corresponds to a corresponding character, used to store the corresponding ASCII code, such a file can be displayed on the screen, and people can read directly to understand Binary files are represented by a series of numbers 0 or one;
3. When it comes to reading and writing programming, the general process is "open the file, read and write the file, close the file", first from "File Open Way";
File open Way table
RT Read Only open a file, only allow read file
wt write only open or create a text file, only allow write file at append open a text file and write data at end of file
RB Read only open a binary file, only allow read Data
WB write only open or create a binary file, only write Data
ab append to open a binary file, only allow reading and writing rt+ read and write
open a file file , allow reading and writing wt+ read and write
to open or create a text file, allow read and write at+ read and write
open a text file, allow reading, or append data at the end of the file
rb+ Read and write open a binary file, allow read and write
wb+ Read-write open or create a binary file that allows read and write ab+ reading and writing
open a binary file, allow read, or append data at the end of the file
4. Classification of standard I/O functions
because there are different requirements in the read and write programming of a file, such as writing only one character, or string, to a file, there are many I/O functions in the file read and write;
Common:
File function category function name open and close file fopen ()/fclose () character read-write function fgetc ()/FPUTC () string read-write function fgets ()/fputs () Data block (block device) read-write function fread ()/fwrite () format read-write fprintf ()/fscanf () Gets the location of the current file stream Ftell () the read-write location of the mobile file stream fseek () check Whether the file read/write ends feof () The following functions:-------------Open File-------------------------------------------------1.
fopen function: Opens the File function prototype according to the path of the file: file *fopen (const char *path,const char *mode); Function Description: Path: File open paths; mode: The state of the stream, that is, how the file was opened; function return value: Success: Returns a file pointer to the file stream failed: return null, 2.fdoopen function: Convert file descriptor to file pointer function prototype: *F
Dopen (int fildes,const char *mode) function Description: Fdopen () Converts the file descriptor of the parameter fildes to the corresponding file pointer and returns; parameter description: Fildes is the file descriptor; mode represents the file pointer; function return Value: Success: Returns a file pointer to the stream after successful conversion: null, 3.freopen function: Freopen () streams the file opened by the original stream to the closed, and then opens the file for the parameter path; function prototype: file* FRE Open (const char *path,cosnt char *mode,file *stream) Parameter description: path is the file path name, mode: File open mode, stream: originally open file stream; 4.fileno function: Returns the file stream File descriptor function Prototype: int Fileno (file *stream) function arguments and return values: stream: A pointer to a file that is already open; 5.fclose function: Turn off the file function prototype: inT fclose (FILE *stream) function return value: Success: return 0; Failed: Return EOF example: #include <stdio.h> #include <stdlib.h> int main () {FILE
*FP = NULL;
FILE *FQ = NULL;
fp = fopen ("/etc/passwd", "R");
if (!FP) {perror ("Open file error.");
Exit (1);
printf ("File descriptor for the first time you open the file:%d\n", Fileno (FP));
fp = freopen ("/etc/vim", "R", FP);
if (!FP) {perror ("Open file2 error.");
Exit (1);
printf ("File descriptor for second open file:%d\n", Fileno (FP));
FQ = Fdopen (0, "w+");
fprintf (FQ, "%s\n", "Hello Wrold");
Fclose (FQ);
Fclose (FP);
return 0;
Results: 3 3 Hello World
5. Character read-write function Fputc/fgetc/getchar/puchar
To be continued .....
Here to write the code piece