From

xiaoxiao2021-03-06  63

Word Source VC World - C language classroom

Self-archiving, also dedicated to the same Delphi programmer as me

file

The basic concept of the file so-called "file" refers to an ordered collection of a set of related data. This data set has a name called file name. Files can be divided into two types of ordinary files and device files.

Ordinary file refers to a ordered data set residing on a disk or other external medium, for a source file, a target file, an executable program that can be referred to as a program file, and the input / output data can be referred to as a data file.

The device file refers to various external devices associated with the host, such as a display, printer, keyboard, and the like. In the operating system, the external device is also asked as a file to manage, and the input, the output is equivalent to reading and writing of the disk file.

From the way of file coding, files can be divided into ASCII code files and binary code files.

The ASCII file is also known as a text file, which is stored in the disk, and each character corresponds to one byte, which is used to store the corresponding ASCII code. For example, the storage form of number 5678 is: ASC code: 00110101 00110110 00110111 0011000 ↓ ↓ ↓ 10 feed code: 5 6 7 8 Total 4 bytes. The ASCII code file can be displayed on the screen, such as the source file is an ASCII file, with the DOS command Type to display the contents of the file. Since it is displayed in characters, you can read the contents of the file.

Binary files are stored in binary encoding methods. For example, the number of storage forms of 5678 is: 00010110 00101110 Only two bytes. Although binary files can also be displayed on the screen, its content cannot be read. The C system is in handling these files, and it is not distinguished, which is regarded as a character stream, and is processed by byte. The start and end of the input / output character stream is only controlled by the program and is not controlled by the physical symbol (such as the carriage. Therefore, this file is also called "streaming file".

This chapter discusses various operations such as opening, closing, reading, writing, and positioning of flow files. The file pointer points to a file with a pointer variable in the C language, which is called a file pointer. The files you refer to the files can be made in various operations. Defining the general form of the file pointer: file * pointer variable identifier; where the file should be uppercase, it is actually a structure defined by the system, which contains information such as file name, file status, and file current location. Don't care about the details of the File structure when writing a source program. For example: file * fp; indicates that the FP is a pointer variable to the File structure. You can find the structural variable of a file information through the FP, and then find the file according to the information provided by the structural variable, and implement the file. It is customary to call the FP as a pointer to a file. The open and closing files of the file are turned on before reading and writing, and the use is to be turned off. The so-called open file is actually a variety of information for establishing a file, and makes the file pointer to the file for other operations. The shutdown file is disconnected between the pointer and the file, or the file is prohibited.

In the C language, the file operation is done by the library function. The main file operation functions will be introduced in this chapter.

File Open Function FOpen

The FopEN function is used to open a file, the general form of the call is: File pointer name = fopen (file name, use file mode) where "file pointer name" must be illustrated as a file type pointer variable, "file name" It is the file name that is opened. "Using file mode" refers to the type and operational requirements of the file. "File Name" is a string constant or a string array. For example: file * fp; fp = ("File A", "R"); its meaning is to open the file file a in the current directory, only "read" operation is allowed, and the FP points to the file. Another example: file * fphzkfphzk = ("C: // HZK16 '," RB ") It is whose meaning is to open the file HZK16 in the root directory of the C drive disk, which is a binary, which is only allowed to perform read operations in binary. The first representation of the second anti-slant line "//", the second represents the root directory. There are 12 ways to use the file, and their symbols and meaning are given below. File usage meaning "RT "Read-only opens a text file, only allow reading data" WT "only writes to open or create a text file, only allowing writing data" AT "to add a text file, and write data" RB "on the end of the file, only open one Binary file, only allow reading data "WB" only written or create a binary file, only allowing writing data "ab" to add a binary file, and write data "RT " in the file end data "RT " to open a text file, allow reading Write "WT " read and write open or create a text file, allow reading and writing "AT " to open a text file, allow reading, or add data "RB " to read a binary file, allow reading and write "WB " reads and writes to open or create a binary, allow reading and writing "AB " to open a binary file, allow reading, or add data in the file to the following description: 1. File usage Made from R, W, A, T, B, Six characters, the meaning of each character is: R (read): Read W (WRITE): Write a (append): Add T (Text): Text file, Can not write B (banary): binary : read and write

2. When using "R" to open a file, the file must already exist and can only be read from the file.

3. The file opened with "W" can only be written to the file. If the open file does not exist, establish the file with the specified file name. If the open file already exists, remove the file and rebuild a new file.

4. To add new information to an existing file, you can only open the file in a "A" mode. But this file must be existing, otherwise it will be wrong.

5. When opening a file, if an error is wrong, the FOpen will return a null pointer value NULL. This information can be used in the program to discriminate whether the operation of the open file is completed and processed accordingly. So open the files frequently: IF ((fp = fopen ("C: // HZK16", "RB") == null) {Printf ("/ Nerror On open c: // hzk16 file!"); Getch (); EXIT (1);} The meaning of this program is that if the pointer returns is empty, it means that the hizk16 file in the root directory cannot be opened, then the prompt information "Error ON Open C: / Hzk16File!" The function of the next line getch () is a character from the keyboard, but is not displayed on the screen. Here, the role of this line is waiting, only the program continues to execute when the user is knocking from the keyboard, so the user can take this Waiting time reading error message. After the key is keys to execute the exit (1) exit program. 6. When you read a text file into memory, you should convert the ASCII code into a binary code, and the file is written in text to write a disk, Convert binary code to ASCII code, so the reading and writing of text files takes more conversion time. There is no such conversion to the reading and writing of binary files.

7. Standard input file (keyboard), standard output file (display), standard error output (error message) is opened by the system and can be used directly. File Close Function FCLOSE File Once used, the application closes the file to turn the file to avoid errors such as data loss of the file.

Fclose function

The general form of call is: Fclose (file pointer); for example: fclose (fp); Fclose function returns to 0 when turning off file operations normally. If a non-zero value is returned, there is an error. The reading and writing of the file is the most common file operation.

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

New Post(0)