C language: file operation (1)

table of Contents:

  1. C file overview
  2. File type pointer
  3. File opening and closing
  4. File reading and writing
  5. File location
  6. Error detection
  7. File input and output summary

The so-called file refers to an ordered collection of related data. This data set has a name called the file name. In fact, we have used files many times, such as source files, object files
, executable files, library files (header files), etc.

Files usually reside on external media (disks, etc.) and are transferred to memory when they are used. Documents can be classified differently from different angles. From the user's point of view, files can be divided into two types: ordinary files and device files.

The operating system manages data in units of files.
Regardless of whether it is input or output, a file has its own buffer.

From the user's point of view:
special files (standard input, output files or standard device files)
ordinary files (disk files)

From the perspective of the operating system:
each input and output device connected to the host is regarded as a file.
For example: input file: terminal keyboard.
Output files: display screen and printer

Classification of documents:
According to the organization of data:

  1. ASCII file: put an ASCII code for each byte
  2. Binary file: The data in the memory is output to the disk for storage according to its storage form in the memory.

The ASCII file is convenient for processing characters one by one, and also for outputting characters. However, it generally occupies more storage space and takes time for conversion.
Binary files can save external storage space and conversion time, but a byte does not correspond to a character, and the character form cannot be output directly.
Generally, the intermediate result data needs to be temporarily stored in the external memory, and if it needs to be input into the memory in the future, it is usually stored in a binary file.

C language processing method for files:
buffer file system: the system automatically opens a buffer in the memory area for each file being used. The input and output with the buffered file system is also called advanced disk input and output.
Unbuffered file system: The system does not automatically open up a buffer of a certain size, but the program sets the buffer for each file. The input and output with the unbuffered file system is also called the low-level input and output system.

File opening and closing

File pointer variable: FILE *fp;

fp is a pointer variable pointing to a FILE type structure. We make fp point to the structure variable of a certain file, so that the file can be accessed through the file information in the structure variable.

If there are n files, generally n pointer variables should be set to make them point to several files respectively to achieve file access.

Array of FILE type:

FILE f[ 5]; Defines a structure array f, which has 5 elements and can be used to store information about 5 files.

1. File opening (fopen function)

Function call:

FILE *fp ;

fp= fopen (file name, use file method);

note:

o The name of the file to be opened, that is, the name of the file to be accessed. 0 The way the file is used ("read" or "write", etc.);

o Let which pointer variable point to the opened file.

There are the following instructions for the use of files

When opening a file with "r, the file must already exist and can only be read from the file.

The file opened with "w" can only be written to this file. If the opened file does not exist, the file is created with the specified file name. If the opened file already exists, the file is deleted and a new file is rebuilt.

If you want to append new information to an existing file, you can only open the file in "a" mode. However, the file must exist at this time, otherwise an error will occur.

When opening a file, if an error occurs, fopen will return a null pointer value NULL.

This information can be used in the program to judge whether the work of opening the file is completed, and deal with it accordingly.

When reading a text file into the memory, the ASCII code must be converted into binary code, and when the file is written to the disk in text mode, the binary code must also be converted into ASCII code, so the reading and writing of the text file will cost more Conversion time. There is no such conversion for reading and writing binary files.

Guess you like

Origin blog.csdn.net/yooppa/article/details/112862308