C language review file operation (on)

xiaoxiao2021-03-06  39

File overview

1. File: The collection of related data stored on the external medium.

For example, the program file is a collection of program code; the data file is a collection of data.

2, the file name: The operating system is managed by the file as a file, each file has a name, the file name is the logo of the file, and the operating system accesses the file via the file name.

For example, look up, open the file, and read or write data.

3, disk file, device file:

(1) Disk file: The file is typically saved on a magnetic medium (such as a floppy disk, hard disk), so it is called a disk file. (2) Equipment file: The operating system will often be connected to the host (such as keyboard - input file, display, printer - output file) as a file, ie device file. Many disk files concept, operation, and device files are also meaningful, valid.

4, ASCII file, binary:

Document can be divided into ASCII files and binary files according to the form of the file. (1) ASCII file (text file): Each byte stores an ASCII code that represents a character. The ASCII file can be read and can be printed, but it needs to be converted with memory data. (2) Binary document: Output data in memory according to its memory in memory, and saved in the file. Binary files take up less space, memory data and disk data exchange without conversion, but binary files are not read, print.

For example: the same integer 10000, if saved in a text file, you can read the text editor such as NotePad, Edit, or use Type under DOS, it takes 5 bytes; if saved in the binary, you can't Read, but we know that an integer is represented by complement in memory and occupies 2 bytes, so if you save 2 bytes in the binary. The text file / binary file is not determined by the suffix, but is determined by the content, but the file suffix often implies its categories, such as * .txt represents text files, *. Doc, *. Bmp, *. EXE binaries .

5, buffer file system, non-buffered file system:

(1) Buffer file system: The system automatically opens up a buffer for each file in memory. When reading data from disk, some data is input from the disk file to the memory buffer (full buffer), and then give data from the buffer one by one; when the data is output to the disk file, the data is sent first. The memory buffer is filled with the buffer to output it together. Reduce the actual access (read / write) number of disks. ANSI c only uses a buffer file system. (2) Non-buffered file system: The buffer is automatically set by the system, and the user is set as needed.

In the C language, there is no input and output statement, and the read and write to the file is implemented with the library function.

File type pointer

1. File Type (Structure) - File Type File type is a structural type, defined in stdio.h, and is used to store the current information about files.

2, file pointer variable (file pointer) is usually done to the File structure through the File type pointer variable (referred to as the file pointer), after opening a file, the system opens up a file variable and returns the file pointer of this file; This file pointer is saved in a file pointer variable, and all the operations for files are completed by this file pointer variable; until the file is turned off, the file pointer points to the file type variable. For example: fp = fopen ("MyData.txt", ...) When the file opens, the system opens up a file variable and returns the file pointer, assigns this pointer (saved) to the file pointer variable fp * / fclose ( fp); / * Close the file, release the file pointer FP point to file variable * /

Steps to the operation of the file: Open first, read and write, and finally close.

Open file (Fopen function)

1. After the file is opened, the file can be operated, and the file is turned on by calling the fopen function. The format that calls the fopen is: Open (file name, open mode or usage);

Note: You must assign the file pointer to the file to the file pointer variable.

For example: file * fp; fp = fopen ("d: //a1.txt", "r");

Document opens must check the return value of the FOPEN function. Because it is possible that the file cannot be opened normally. The fopen function returns NULL when it is not open. You can check: if ((fp = fopen (...)) == null) {Printf ("Error Open file / n"); Return;}

Explanation: (1) Open the D: The file name is a1.txt file in the root directory, the open mode "R" means read only. (2) Fopen function returns a file pointer to D: /A1.txt, and assigns the value to the FP, fp-> this file, that is, the FP is associated with this file. (3) The file name contains the file name. Extension; path to "//" means. (4) The file open mode contains the following types of keywords indicating open mode, and different classs can be combined. "R": You can only read data from the file and write data to the file. This method requires that the file that wants to open has existed. "W": You can only write data to files and cannot read data from files. If the file does not exist, create a file, if the file exists, the original file is deleted, then recreate the file (quite overwritten the original file). "A" method: add data at the end of the file without deleting the original file. This method requires that the file that wants to open has existed. " " ("R , W , A "): Both readable, writable. However, "R ", "A " requires that the file already exists, "W " Note; "R " When the file pointer points to the beginning, "A " opens the file, indicates the end of the file. "B, T": Open files in binary or text. The default is text, T can be omitted. When reading a text file, convert the "Enter" / "Put" into a "wrap"; write the "Wrap" to "Enter / Retaway" when writing a text file. (7) When the program starts running, the system automatically opens three standard files: standard input, standard output, standard error output. Generally, these three files correspond to the terminal (keyboard, display). These three files don't need to be manually opened, you can use it. Standard file: Standard input, standard output, standard error output correspondence file pointer is stdin, stdout, stderr. File close (Fclose function)

The file must be turned off after the file is used to avoid data loss. Format: Fclose (file pointer);

Document reading and writing

Common file read and write functions: 1. Character read and write functions: fgetc, fputc2, string read and write function: fgets, fputs3, format read and write functions: fscanf, fprintf4, data block read and write functions: FREAD, FWRITE Description: 1 These functions begin with f (file). 2, these functions must use the file pointer obtained when opening the file. 3, the first three types of functions are basically the same as the standard I / O function. 4, generally, use "order access" mode for text files, use "random access" mode for binary files.

Affirmation: The above content is from the Changzhou Information Vocational and Technical College, the author has been revised for your own learning.

转载请注明原文地址:https://www.9cbs.com/read-75900.html

New Post(0)