C language file and input and output principle

      If you want to save data continuously, you must use external storage devices, such as disks. The programming language provides the function of accessing and using the external storage. The external storage information is stored in the file system under the management of the operating system and organized by means of files and directories. A file is a group of encapsulated data, and a directory is a subdirectory and a file. collection. Directories or files can be used by name. The program deals with external storage during execution, mainly accessing and using various files as external storage information entities.

       The objects of program input and output can be various devices, such as keyboards, monitors, printers, etc., or files. Many operating systems adopt a unified point of view, and unify operations related to input and output into the concept of "file", and treat devices such as keyboards and monitors as files. Given a "file name", all operations on them are performed through " filename" to proceed.

       The c language does not have a language structure dedicated to input and output. ANSI C uses file and input and output functions as part of the standard library to improve program portability, and the mechanisms related to input and output are unified to the concept of files. The standard header file <stdio.h> describes the data structures and functions in this regard.

What is a stream?

       Files are the objects of input and output. In order to exchange information with files, it is necessary to establish a connection with them, and the stream is such a connection. This action of establishing a connection (creating a stream) is vividly called opening a file. In order to input from a file, the program needs to create an input stream associated with the file. Similarly, to output to a file, an output stream associated with it must be established. Of course, it is also possible to create streams that can both input and output.

 Standard streams are divided into two categories: character streams and binary streams. The so-called character stream regards the file as a sequence of character lines, each line contains 0 or more characters, and there is a newline character '\n' at the end. The binary stream stores the data directly into the file in the form of the memory. The binary stream operation ensures that the data is written into the file and then read back in the same way. The form and content of the data will not change. Since the operation does not convert the data form, There are speed advantages when storing and loading large data, but this form of storage is not suitable for human reading.

How is the stream implemented?

Streams are implemented through a special data structure for which the standard library defines the type FILE

The following is MinGw's description of the structure of FILE in the library stdio.h

struct _iobuf {
 
char *_ptr;
 
int _cnt;
 
char *_base;
 
int _flag;
 
int _file;
 
int _charbuf;
 
int _bufsiz;
 
char *_tmpfname;
};

typedef struct _iobuf FILE;

The following is the structure description of FILE in the library corecrt_internal_stdio.h by VC

struct __crt_stdio_stream_data

{

    union

    {

        FILE  _public_file;

        char* _ptr;

    };

   

    char*            _base;

    int              _cnt;

    long             _flags;

    long             _file;

    int              _charbuf;

    you _bufsiz;

    char*            _tmpfname;

    CRITICAL_SECTION _lock;

};

       This type of object is used to save all information related to the stream, in fact, this information is transparent to us and we don't need to care about it. It only needs to be concerned that the operation of opening a file returns a pointer to FILE, called a file pointer, representing the created stream. All operations on this stream (corresponding file) are performed through this pointer. It can be considered that this kind of file pointer is the embodiment of stream, and people also regard file pointer as a synonym for stream.

Each c program automatically creates three streams (file pointers) when it starts: standard input stream (pointer name stdin), standard output stream (pointer name stdout) and standard error stream (pointer name stderr).

stdin is usually connected to the standard input of the operating system (eg: keyboard)

stdout is usually connected to the standard output of the operating system (eg: monitor)

stderr is usually connected directly to the display and cannot be redirected.

Buffered I/O

The speed of the external memory is relatively slow, and the block transfer method is generally adopted to transfer a batch of data at a time. The data used in the program is often different. In order to bridge the gap between the program and the external data operation mode and speed, people propose a technology, that is, open up a storage area (called data buffer, buffer for short), as a file and A transfer medium between programs that use data. The introduction of the buffer is mainly to improve the work efficiency of the program.

 Input stream operation: The data in the file is copied to the buffer in blocks. When the program needs to read data, it will read from the buffer. It is not necessary to access the external storage every time. If the data to be read has been used up, the system will automatically fetch it from the file. A batch of data fills the buffer.

Output stream operations: Automatically perform block write operations to the file each time the buffer is full.

After the program opens the file (fopen()), the system allocates a buffer (usually dynamic storage allocation) for it, and the data transfer is carried out through the buffer, and the buffer is released when the file is closed (fclose())

For the actual operation of the file, we will talk about it in the next article.

Guess you like

Origin blog.csdn.net/chinagaobo/article/details/124057535