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 are the file operation methods in C language?

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

Share

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

This article mainly introduces the C language file operation methods, 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.

What is a file program file

In fact, from the point of view of the function of files: our files can be divided into two types of files: program files and data files.

Take our C language program as an example, including program files, that is, files with the suffix .c; object files, that is, files with the suffix .obj in the windows environment; and executable files, that is, files with the suffix .exe.

Data file

Data files generally refer to the files used to store the data we need to read and write when using the application.

The operation of the file we are talking about also refers to the operation of the data file.

File name

The file name usually consists of three parts:

File path + file name backbone + file suffix

Why use files?

When we run a program, we always generate some data, and if we don't store the data, it will disappear when we wait for the program to finish.

For example: in the simple address book (simple address book) we wrote last time, if we run the program this time and enter the information, but do not save the data, after closing the program, run the program again next time. You can't see the previous data.

What we want is to record the information in the address book, and only when we choose to delete the data will the data cease to exist.

And this involves the problem of data persistence. Our general methods of data persistence are storing data in disk files and storing data in data.

Library and other ways.

Using files, we can store the data directly on the computer's hard disk, so that the data can be persisted.

Operation file buffer of the file

The C language uses the "buffer file system" to deal with text files and binary files.

The buffered file system means that the system automatically opens a "file buffer" in memory for each file being used in the program.

This file buffer acts as a "middleman" and acts as a transition in the program data area and hard disk files.

File pointer (FILE)

In the buffered file system, the key concept is "file type pointer", or "file pointer" for short.

Each used file opens up a corresponding file information area in memory to store information about the file (such as the name of the file).

Word, file status and current location of the file, etc.). This information is stored in a structural variable. The structure type is systematically declared and is named FILE.

When you use this structure type to define a file pointer, you need to include a header file.

It is important to note that the FILE types of different C compilers do not contain exactly the same content, but are more or less the same.

Whenever you open a file, the system automatically creates a variable of the FILE structure according to the condition of the file and populates the information in it.

As users, we don't have to care about details.

We usually use a pointer to FILE to maintain the variable of the FILE structure, which is more convenient to use.

FILE* pf;// defines a 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.

Opening and closing of files

We should open the file before reading and writing to it, and close the file after using it.

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 specifies that the fopen function is used to open the file and fclose to close the file.

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

This function is included in the header file and is used to open the file.

The first parameter in the list of parameters is the name of the file, and the second parameter is how the file is opened.

If the opening is successful, we can use a file pointer to receive the return value to manage the file; if the opening fails, a null pointer is returned.

Therefore, when we receive the return value of the function, we should add a step to verify that the pointer is null.

Fclose

This function is also included in the

When we finish using the file, remember to close the file.

We can pass in the file pointer as an argument through the fclose function.

The function returns 0 if the shutdown is successful, and EOF (- 1) if the shutdown fails.

How the file is opened

There are different ways to open files, and different ways to open files correspond to different operations.

The meaning of the use of the file if the specified file does not exist "r" (read-only) in order to input data, an error occurred in opening an existing text file "w" (write-only) in order to output data, open a text file and create a new file "a" (append) to the end of the text file to create a new file "rb" (read-only) to enter data Error opening a binary file "wb" (write only) to output data, open a binary file to create a new file "ab" (append) add data to the end of a binary file error "r +" (read and write) in order to read and write, open a text file error "w +" (read and write) in order to read and write, suggest a new file to create a new file "a +" (read and write) to open a file Read and write at the end of the file to create a new file "rb+" (read and write) to open a binary file error "wb+" (read and write) to read and write, create a new binary file to create a new file "ab+" (read and write) Open a binary file, read and write at the end of the file to create a new file

Demo:

# include int main () {FILE* pFile; / / Open file pFile = fopen ("myfile.txt", "w"); / / File operation if (pFile! = NULL) {fputs ("fopen example", pFile); / / close file fclose (pFile);} return 0;}

We will see that if w is used to open the file, the program will look for the file you want to open in the directory of the source file where the code is located, and if the file does not exist, it will automatically create one for you.

However, if the open method used is r, if the program finds that the file does not exist in the directory, it will make an error.

Examples are as follows

Int main () {FILE* pFile; / / Open the file pFile = fopen ("cmyfile.txt", "r"); / / File operation if (pFile = = NULL) {perror ("fopen"); return-1;} fputs ("fopen example", pFile) / / close the file fclose (pFile); return 0;}

An error occurs when the file does not exist in the folder of my source file, and I print out the error message through the function perror.

It is important to note that, just like the above chestnut, only the file name is written. The suffix name is found by default under the file where the source file is located, and if you need to specify a directory, you need to precede the file name with a file path.

For example

# include int main () {FILE* pFile; / / Open file pFile = fopen ("c:/program/test/myfile.txt", "w"); / / File operation if (pFile! = NULL) {fputs ("fopen example", pFile); / / close file fclose (pFile) } return 0;}

Be careful

In C language, if you want to enter a path, we usually use a slash (/) instead of a backslash (\), because the backslash is the symbol of escape, and it is very likely that there happens to be something in your path that can be escaped. this will lead to failure. If you want to use a backslash, use two backslashes together.

These are the basic operations when we use a file, first to open the file, then to manipulate the file, and finally to close the file.

Related concepts of flow

Flow is a highly abstract concept, we can understand that it is equivalent to a channel, just like the current, the input flow can be regarded as an input channel, and the output stream can be regarded as an output channel.

Stream, this channel, connects programs and external devices.

When an external device needs to input data into a program, it needs an input stream.

When a program needs to output data to an external device, it needs an output stream.

According to my personal understanding,

The standard input stream is the channel that connects the program to the standard input device (keyboard).

The standard output stream is the channel that links programs to standard output devices (screens).

Sequential reading and writing of files

Sequential read and write, as the name implies, is the operation of reading and writing in sequence.

Function name applies to character input function fgetc all input stream character output function fputc all output stream text line input function fgets all input stream text line output function fputs all output stream format input function fscanf all input stream format output function fprintf all output stream binary input fread file binary output fwrite file fputc

This function is contained in the function, which is entered character by character.

The first parameter is the character we want to enter, and the second parameter is the input stream.

We can understand it this way.

Through this function, we put the character we want to enter into the corresponding stream, and then the stream will send the character to the corresponding device.

If we write a standard output stream, the data will be output to the screen and we can see it on the console.

Fgets

This function is also included in the function, which gets a character from the stream.

Thank you for reading this article carefully. I hope the article "what are the ways to operate files 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