Basic knowledge of document operation based on C language

Source: Internet
Author: User

As we all know, in C, file operations are done by library functions. Here will introduce to you the input and output functions, for reference.

Because the program often has a large number of input and output operations on the file, it often forms the main part of the program, so the C language provides a lot of input and output functions, which are used for two types of file input and output systems: the buffer file defined by ANSI standard (also known as standard file (stream) input/output (I/O) system); The other is a non-buffered file (also known as a non-standard file (streaming) input/output (I/O) system) that is not defined in the ANSI standard.

We are already familiar with some of the functions of input and output through the keyboard and the monitor, such as scanf (), printf () and so on, these through the console (keyboard, monitor, etc.) for I/O operations, can be seen as a standard file input and output system of some special cases, in fact, in the standard input and output system of some functions , the parameters of the file (file structure pointers or stream pointers), as long as the flow pointers of standard devices are substituted, these standard input and output functions become console I/O functions.

When any program executes, the C system defines 5 standard device files to use. The file structure pointers for the 5 standard device files that are automatically opened (in standard I/O systems) and file names will have a specified value:

The stream pointer name in the device standard file I/O system is not a standard file ...

Keyboard (standard input) stdin 0

Monitor (standard output) stdout 1

Monitor (standard error) stderr 2

Serial port (standard auxiliary) Stdoux 3

Printer (standard printing) Stdprn 4

In this way, whether in standard file systems or non-standard file systems, file structures can be used for console devices as long as they are replaced by the flow pointer or file code above.

Text stream and binary stream

The concept of flow (stream) is introduced in C. It sees the input and output of the data as the inflow and outflow of data, this way, whether it is a disk file or a physical device (printer, monitor, keyboard, etc.), can be viewed as a source and purpose of the stream, depending on their specific physical structure, that is, the operation of the data inflow and outflow.

This kind of data input and output operations object, abstract into a stream, regardless of its specific structure of the method is very conducive to programming, and the flow of output operation function can be used for various objects, and its specific entities, that is, the universality.

There are two types of streams in C: text stream and binary stream (binary stream). The so-called text flow refers to the flow of data in the stream is in the form of characters. In the text stream, ' n ' is replaced by a carriage return CR and the code for LF, 0DH and 0AH. When output, the 0DH and 0AH are replaced with ' n '.

Binary flow refers to the flow of binary numeric sequence, if there are characters in the stream, then a byte of binary ASCII code, if the number, the binary number of a byte is expressed. The n symbol is not transformed when the inflow and outflow occurs. For example, the number 2001 is represented in the text stream by its ASCII code:

' 2 ' 0 ' 0 ' 1 '

| | | |

50 48 48 49

A total of 4 bytes. In the binary stream, it is represented as: 00000111 11010001 with hexadecimal is 07d1. Accounts for only two bytes.

It is shown that the binary flow is more space-saving than text flow, and does not need to convert to N, which can greatly speed up the flow and improve the efficiency. Therefore, for digital streams containing a large number of digital information, binary flow can be used; for streams containing a large number of character information, text flow is used.

Ii. Flow and documentation

In the C language is a form of file, which actually represents a file or device (in general, the device is also a file). The stream as a file always feel not accustomed to, so some people call this and stream equivalent file for streaming files, flow of input and output is also called file input and output operations. When you stream to disk and become a file, means that you want to start a disk write operation, which flows to a character (text stream) or to a byte (binary stream) to start the disk operation, greatly reducing the transfer efficiency (disk is a slow device), and reduce the life of the disk.

To this end, the C language used in the use of the input and output buffer technology, that is, in memory for the input of the disk file opened a buffer (the default is 512 bytes), when the flow to the buffer filled, then start the disk once, the buffer contents into the disk file. Reading files is similar.

This type of file input-output operation is referred to as standard input output in C language, or streaming input output (because this input-output operation is the standard ANSI C recommended). There is also a buffer file input output, called non-standard file input output or low-level input output, it will be directly managed by DOS. The next section on these two input-output file systems is described.

III. data structure of file files

This is the definition used in Turbo C (in the Stdio.h file), the different C compilers may use different definitions, but the basic meaning does not change too much.

Flags: is a 10-digit sign with its specific meaning as follows:

For the other fields and the Flags field, please refer to some other information. It's not much to say here, it's not what we're asking for at the moment.

It should be noted that you do not confuse file pointers with files structure pointers, which represent two different addresses. The file pointer indicates the location of the data currently being read and written to the file, and the file structure pointer indicates the address of the file structure that corresponds to the open document in memory, and it actually contains information about the file pointer. The fields in the stream pointer are used internally by the C language, and the user should not access any of its fields.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.