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

How to realize File Operation in C language

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

Share

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

This article mainly introduces how to achieve file operation in C language, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Why use files

We are thinking that since it is an address book, we should record the information, and only when we choose to delete the data will the data cease to exist. This involves the problem of data persistence. Our general methods of data persistence include storing data in disk files, storing data in databases, and so on. Using files, we can store the data directly on the computer's hard disk, so that the data can be persisted.

What is a file?

The files on disk are files.

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 file

Includes source program files (suffix .c), destination files (windows environment suffix .obj), and 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.

This chapter discusses data files.

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

Open and close file pointers to files

Each used file opens a corresponding file information area in memory, which is used to store the relevant information of 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

For example, the stdio.h header file provided by the VS2013 compilation environment has the following file type declaration:

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

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.

For example:

Fopen and fclose functions / / Open the file FILE * fopen (const char * filename, const char * mode); / / close the file int fclose (FILE * stream)

Mode of use:

Generally speaking, just learn to operate the first three skillfully.

Example:

/ * fopen fclose example * / # include int main () {FILE * pFile;// opens the file pFile = fopen ("myfile.txt", "w"); / / File operation if (pFileoperations null) {fputs ("fopen example", pFile); / / closes the file fclose (pFile);} return 0;} sequential reading and writing of files

Note: use these functions to include header files

Thank you for reading this article carefully. I hope the article "how to operate documents in C language" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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: 0

*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