Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the operation flow of C language files?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the operation flow of C language files". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "what is the operation flow of C language files" can help you solve the problem.

Why use files

In the address book written earlier, after increasing the number of people to exit the program, the data will disappear. At this time, the data is stored in memory, and the next time you run the address book program, the data will have to be re-entered, which is very difficult to use.

So file operation arises at the historic moment. There are two ways to persist data: 1. Store the data in disk file 2. When we store the files in the database, we can store the data directly on the hard disk of the computer, thus achieving the persistence of the data.

What is a file?

But in programming, we generally talk about two kinds of files: program files and data files (classified from the point of view of file function).

Program files: include source program files (suffix .c), target files (windows environment suffix .obj), executable programs (windows environment suffix .exe).

Data file: the content of the file is not necessarily the program, but the data read and written when the program is running, such as the file from which the program needs to read the data, or the file from which the content is output.

The input and output of the data processed in the previous chapters are all based on the terminal, that is, the input data from the keyboard of the terminal, and the running results are displayed on the display.

In fact, sometimes we will output the information to the disk, and then read the data from the disk to memory when needed, here we are dealing with the files on the disk.

File name

A file should have a unique file identity so that users can identify and reference it.

The file name contains three parts: file path + file name backbone + file suffix

For example: C:\ code\ test.txt

For convenience, a file ID is often called a file name.

Some concepts about files

File pointer: in a buffered file system, the key concept is "file type pointer", or "file pointer" for short.

File information area: each used file opens a corresponding file information area in memory, which is used to store information about the file (such as the name of the file, the status of the file and the current location of the file, etc.). This information is stored in a structural variable. The structure type is systematically declared and is named FILE.

The following file types are declared in the stdio.h header file provided by the VS2013 compilation environment

Struct _ iobuf {char * _ ptr; int _ cnt; char * _ base; int _ flag; int _ file; int _ charbuf; int _ bufsiz; char * _ tmpfname;}; typedef struct _ iobuf FILE

Whenever you open a file, the system will automatically create a variable of the FILE structure according to the situation of the file, and fill in the information in it, so that the user does not have to pay attention to the details.

FILE pointer: generally, a pointer to FILE is used to maintain the variable of the FILE structure, which is more convenient to use.

Below we can create a pointer variable for FILE*:

FILE* pf;// file pointer variable

Defines pf as a pointer variable to FILE-typed data. You can make pf point to the file information area (which is a structural variable) of a file. The file can be accessed through the information in the information area of the file. That is, the file associated with it can be found through the file pointer variable

Regulation: the file should be opened before reading and writing, and the file should be closed after use.

When writing a program, when you open a file, you will return a pointer variable of FILE* pointing to the file, which is equivalent to establishing a relationship between the pointer and the file.

ANSIC stipulates that the fopen function is used to open the file and fclose is used to close the file.

First of all, understand what reading and writing mean.

Write file: output data from memory to a file or screen.

Read file: enter data from a file or keyboard into memory.

File function fopen

Function prototype:

FILE * fopen (const char * filename, const char * mode)

Function function: Open a file.

The function of this function is to open a file, the first parameter of the function is the file name of the file you want to open, and the second parameter is the form of the file.

Return value: Each of these functions returns a pointer to the open file. A null pointer value indicates an error.

Returns a pointer to a file, or a null pointer if an error is opened.

Note: you need to check the validity of the fopen return value.

FILE* pf = fopen ("data.txt", "r"); if (pf = = NULL) {printf ("% s\ n", strerror (errno)); return;// failure returns} fclose

Function prototype:

Int fclose (FILE * stream)

Function function

Closes a stream (fclose).

Close a stream

Return value: fclose returns 0 if the stream is successfully closed.return EOF to indicate an errorreturn 0

If the stream closes successfully, return EOF (- 1) if it fails.

Example code FILE* pf = fopen ("data.txt", "r"); if (pf = = NULL) {printf ("% s\ n", strerror (errno)); return 1 / failure returns} absolute path

The absolute path is in the location of the specific file. For example:

D:\ c-language\ c yuyan\ c yuyan\ data.txt

FILE* pf = fopen ("D:\\ c-language\\ c yuyan\\ c yuyan\ data.txt", "r")

However, in order to prevent the characters in the string and the characters that follow them from being treated as escaped characters as a whole, you need to add a'\ 'after each'\'.

How the file is opened

The first three are common ways.

Rules:

File opening means "r" (read-only) Open the file for input operation. If it does not exist, an error will be reported. "w" (write only) to output data, open a text file. If it exists, clear the data and output it. If it does not exist, a new file will be created. "a" (append) adds data to the end of the text file. If the file does not exist, it is created. "rb" (read-only) opens a binary file in order to enter data. If it does not exist, an error occurs. "wb" (write only) opens a binary file to output data. If it does not exist, create a new file. "ab" (append) adds data to the end of a binary file. If it does not exist, an error occurs. "r +" (read and write) opens a file for update (input and output). An error occurs if it does not exist. "w +" (read and write) creates an empty file and opens it for update (input and output). If a file with the same name already exists, its contents will be discarded and the file will be treated as a new empty file. "a +" (read and write) opens a file and reads and writes at the end of the file. Create a new file if it does not exist. File operation flow

The following is the general procedure for manipulating files.

# include # include # include int main () {/ / Open the file FILE* pf = fopen ("data.txt", "r"); if (pf = = NULL) {printf ("% s\ n", strerror (errno)); return 0;} / / perform a series of operations on the file. . / / close the file fclose (pf); pf = NULL; empty return 0;} this is the end of the introduction on "what is the operation flow of C language files". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 286

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report