Because the program often has a lot of input and output operations on files, it often forms the main part of the program, so the C language provides a lot of input and output functions, they are used for two types of file input and output systems: Buffer files (also known as standard files (streams) input and output (I/O) Systems) defined by ANSI standards ); another type is non-buffer files (also known as non-standard files (Stream) input/output (I/O) Systems) not defined in ANSI standards ).
We are already familiar with some input and output functions through the keyboard and display, such as scanf (), printf (), etc) i/O operations can be seen as some special cases of the input/output system of standard files. In fact, some functions in the standard input/output system, for file parameters (file structure pointer or stream pointer), these standard input and output functions are console I/O functions as long as they are replaced by the stream pointer of the standard device. During execution of any program, the C system defines five standard device files for use. The file structure pointer (in the standard I/O system) and file code of the five standard device files that are automatically opened will have a specified value:
The stream pointer name in the device standard file I/O system is a non-standard file ......
Keyboard (standard input) stdin 0
Monitor (standard output) stdout 1
Display (standard error) stderr 2
Stdoux 3
Printer (Standard Print) stdprn 4
In this way, the file structure can be replaced by the above stream pointer or file code in both standard and non-standard file systems. These functions are also applicable to console devices.
1. Text Stream and binary stream
The concept of stream is introduced in C. It regards the input and output of data as the inbound and outbound data, so that whether it is a disk file or a physical device (printer, display, keyboard, etc.), it can be seen as a source and destination of a stream, they are the same thing, regardless of their physical structure, that is, the operations on them are the inbound and outbound data. This type abstracts the input and output operation objects of data into a stream. Regardless of its specific structure, the method is very helpful for programming, and the output operation functions involving the stream can be used for various objects, it has nothing to do with the specific entity, that is, it is universal.
In C, the stream can be divided into two categories: Text Stream and binary stream ). The text stream refers to the flow of data in the stream in the form of characters. In the text stream, \ '\ n \' is replaced with the code 0dh and 0ah of carriage return Cr and line feed lf. When the output is complete, replace 0dh and 0ah with \ '\ n \'.
A binary stream is a sequence of binary numbers. If a stream contains characters, it is represented by a single-byte binary ASCII code. If it is a number, it is represented by a single-byte binary number. During inbound and outbound traffic, the \ n symbol is not transformed. For example, the number 2001 is represented by its ASCII code in a text stream:
\ '2 \ '\ '0 \' \ '0 \ '\ '1 \'
|
50 48 48 49
4 bytes in total. In the binary stream, 00000111 11010001 is expressed as 07d1 in hexadecimal format. Only two bytes are occupied.
It can be seen that the binary stream saves space than the text stream and does not need to convert \ n, which can greatly speed up the stream and improve efficiency. Therefore, binary streams can be used for digital streams that contain a large amount of digital information. Text streams can be used for streams that contain a large amount of character information.
Ii. stream and file
In C language, it is a file form, which actually represents a file or device (in a broad sense, a device is also a file ). Streaming As a file is always not used to it. Some people call this file the same as a stream file. The input and output of a stream is also called the input and output operations of a file. When a file is written to a disk and becomes a file, it means that the disk write operation should be started. In this way, the disk operation should be started when a character (Text Stream) or a byte (binary stream) is imported into the disk, this will greatly reduce the transmission efficiency (the disk is a slow device) and reduce the service life of the disk. Therefore, the C language uses the buffer technology in the input and output, that is, a buffer (512 byte by default) is opened for the input disk file in the memory. When the stream to the buffer is full, restart the disk again and load the buffer content to the disk file. Reading files is similar.
In C language, the input and output operations of such files are referred to as standard input and output, or stream input and output (because such input and output operations are the standard recommended by ansi c ). Another type is non-buffered file input and output, which is called non-standard file input and output or low-level input and can be directly managed by DOS. The following section describes the two types of input/output file systems.
Iii. Data Structure of File
Typedef struct
{
Short level;
Unsigned flags;
Char FD;
Unsigned char hold;
Short bsize;
Unsigned char * buffer;
Unsigned char * CURP;
Unsigned istemp;
Short token;
} File;
This is the definition used in Turbo C (in stdio. H files). Different C compilers may use different definitions, but the basic meaning will not change much.
Flags: A 10-digit flag. Its meanings are as follows:
Symbol Meaning
0 _ f_read
1 _ f_writ write
2 _ f_buf releases the buffer zone by fclose
3 _ f_lbuf row Buffer
4_f_err error mark
5 _ f_eof EOF file tail flag
6 _ f_bin binary mode
Input 7 _ f_in
8 _ f_out
The 9_f_term file is a terminal.
For details about other fields and the functions of flags fields, refer to other materials. I will not talk about it here. It is not what we need at present.
Note that do not confuse file pointers with file structure pointers. They represent two different addresses. The file pointer specifies the location of the data currently read and written to the file, and the file structure pointer indicates the address of the file structure corresponding to opening the file in the memory, this pointer actually contains the file pointer information. Each field in the stream pointer is used internally in the C language, and users should not access any of its fields.