Concept and pointer of C language files, and concept pointer of C language files

Source: Internet
Author: User

Concept and pointer of C language files, and concept pointer of C language files
1. Concepts of C language files

In a program, when an input function is called to input data from an external file to a variable in the program, this operation becomes "input" or "read "; when an output function is called to output the values of variables in a program to an external file, this operation is called "output" or "write ".

In C, the input and output data are processed in the form of "data streams. That is to say, the system does not add any information during output. When input, data is read one by one until the EOF or file end mark is reached. The input and output files in the C program are stored in the media as data streams.

The input and output methods of files are also called "access methods ". In C, there are two access methods for files: sequential storage and direct access.

(1) features of sequential file access: When a file of this type is opened and read or write operations are performed, it always reads or writes from the beginning, start, and end to end. That is to say, when you access a file in sequence, you must read the nth byte. First, you must read the first n-1 byte, instead of the nth byte; to write the nth byte, first write the nth byte before.

(2) direct access to a file, also known as a random access file, has the following features: You can call the C-language library function to specify the number of bytes to start reading or writing, then, read the data at this location directly or write the data to this location.

Data can be stored in the media in the form of text or binary, so files can be divided into text files and binary files by the form of data storage.

(1) The so-called text file refers to the conversion of data into a string of characters when output, each character is stored in the file with the ASCII value of the characters, one character occupies one byte. This conversion is performed when the printf function is used for output, and the terminal of the output file is specified as the screen during internal processing. On the contrary, when the input is performed, the specified string of characters is converted into data by type and stored in memory. For example, this conversion is performed when the scanf function is called for input, specify the input terminal as the keyboard during internal processing.

(2) when data is output in binary format to a file, the data is directly stored on the hard disk without any conversion or in the form of storage within the computer. That is to say, for bytes data, each character occupies one byte; for int data, each data occupies two bytes; for float data, each data occupies four bytes. When reading data from a binary file, the data is directly stored in the memory space occupied by the variable without any conversion. As you can see, there is no conversion operation, which increases the input/output speed of the file. Note: binary data cannot be directly output to the terminal screen or input from the keyboard.

The ANSI standard specifies that when an input or output file is input or output, the system opens a buffer for the input or output file. The so-called "buffer zone" is a storage area opened up by the system in memory for each file. When outputting a file, the system first fills in the output data in the buffer opened for the file. When the buffer is filled, the buffer content is output to the corresponding file at a time. When data is input from a file, a batch of data input from the input file is first put into the memory buffer of the file, the input statement reads data from the buffer in sequence. When the data in the buffer is read, a batch of data is input from the input file. In this way, read and write operations do not need to frequently access external devices, thus improving the speed of read and write operations.

2 file pointer

The file pointer actually points to a struct type pointer, which contains the following: the address of the buffer, the location of the currently accessed characters in the buffer, whether the file is "read" or "write", whether an error occurs, and whether the file end mark has been encountered. You do not have to understand the details. Everything is defined in the stdio. h header file. The struct type mentioned above is generally called FILE, and the general format of defining object type pointer variables is:

FILE * pointer variable name;

For example:

FILE * fp1, * fp2;

Both fp1 and fp2 are defined as pointer variables pointing to the file type.

Related Article

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.