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 open and close C language files

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

The editor of this article gives you a detailed introduction of "how to open and close c language files". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to open and close c language files" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

Opening and closing of files

Open a file (fopen function)

FILE * fp

Fp = fopen (file name, using file method)

For example:

FILE * fp

Fp = fopen ("course.txt", "r")

(1) "r" mode:

You can only input data to the program, not to the file. The file must exist.

(2) "w" mode:

You can only export data to a file, not input data to a program.

If there is no file with the specified file name, a new file with the file name is created.

If the file already exists, all the original data in the file will be erased when you open the file.

(3) "a" mode:

Export data to a file, but you cannot open a file that does not exist.

When you open an existing file, the original data in the file will not be erased

After the file is opened, the data output to the file is added to the end of the file.

(4) when using "b": indicates that the operation file is in the form of binary data.

(5) when using "+": it means that after the file is opened, input and output operations can be carried out.

"r" mode:

You can only input data to the program, not to the file. The file must exist.

(2) "w" mode:

You can only export data to a file, not input data to a program.

If there is no file with the specified file name, a new file with the file name is created.

If the file already exists, all the original data in the file will be erased when you open the file.

(3) "a" mode:

Export data to a file, but you cannot open a file that does not exist.

When you open an existing file, the original data in the file will not be erased

After the file is opened, the data output to the file is added to the end of the file.

(4) when using "b": indicates that the operation file is in the form of binary data.

(5) when using "+": it means that after the file is opened, input and output operations can be carried out.

After using a file, you should close it to prevent errors caused by the program's misoperation of the file. File closing refers to revoking the file information area and file buffer so that the file pointer variable no longer points to the file.

Close the file with the fclose function:

Fclose (file pointer)

For example:

Fclose (fp)

If the close file operation is successful, the fclose function returns a value of 0; otherwise, it returns EOF (EOF is defined as-1 in the stdio.h header file).

After the file is closed, if you still need to read and write, you need to reopen the file.

After reading this, the article "how to open and close c language files" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to the industry information channel.

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

Internet Technology

Wechat

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

12
Report