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 file opening mode 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 explains "what is the file opening mode in C language". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what is the file opening mode in C language"?

1. Files are divided into ASCII files and binary files. ASCII files, also known as text files, are composed of a series of characters, and the ASCII code value of each character is stored in the file.

2. FILE * fp

FILE is a file type identifier and a structure type defined by C compiler. The structure contains file name, file status and other information.

It defines a pointer variable fp, which is used to point to a file that holds the first address of the file buffer.

3. File opening: fopen ()

There are 12 ways for C language to access files. It is a combination of these keywords: read,write,append (append), text (text file), banary (binary file), and + means read and write.

4. Close the file: fclose ()

The operating system has a limit on the number of open files, when there are too many open files, the read and write of the files will affect each other, so close the unused files in time.

This function returns 0 when the file is closed normally, otherwise-1

When the file is closed, the system cleans up the buffer pointed to by fp, outputs the data to the disk file, and then releases the buffer unit to decouple the file pointer from the specific file. This prevents files from being lost and information from being destroyed.

But when using C language to operate the file, sometimes there will be garbled code. At this time, you should check your own program, whether the variables written to the file have been initialized, and whether you have closed the file correctly. When the variable is not initialized, the garbled code is often shown as "scalding".

Another possibility is that the way the file is encoded is not consistent with the way the program is encoded. The TXT files under Windows are all encoded by ANSI. Pay attention to the way your compiler encodes.

R stands for read, + for readable and writeable, w for write,b for bit binary bit, t for text

R Open a read-only file, which must exist

R + Open a readable and writable file, which must exist (the write file here refers to overwriting the previous file

Rt opens a read-only text file, which must exist

Rt+ read and write opens a text file that allows reading and writing, and the file must exist (the write file here refers to overwriting the previous file

Rb only opens a binary file, and the text must exist

Rb+ read and write opens a text file that allows reading and writing, and the file must exist (the write file here refers to overwriting the previous file

W open a write-only file. If the file exists, the length of the file will be zero, that is, the contents of the file will disappear, and if the file does not exist, the file will be established.

W + opens the read / write file. If the file exists, the length of the file will be cleared to zero, that is, the file content will disappear. If the file does not exist, the file will be created. (for reading the file here, you also need to use the rewind () function.)

Wt opens a write-only text file. If the file exists, the length of the file is cleared to zero, that is, the content of the file disappears. If the file does not exist, the file is created.

Wt+ opens a read-write text file. If the file exists, the length of the file is cleared to zero, that is, the content of the file disappears. If the file does not exist, the file is established.

Wb opens write-only binary files. If the file exists, the length of the file is cleared to zero, that is, the contents of the file will disappear. If the file does not exist, the file will be established.

Wb+ opens a read / write file. If the file exists, the length of the file is zero, that is, the contents of the file will disappear. If the file does not exist, the file will be established.

An open the write-only file in an additional way, if the file does not exist, create the file, and add data to the end of the file if it exists, that is, append the content

A + opens the read / write file in an additional way, creates the file if it does not exist, and writes data to the end of the file if it does not exist. (for reading the file here, you also need to use the rewind () function, but you do not need the rewind () function to write the file. An is appended.)

Append of at binary data, if it does not exist, it can only be written.

At+ read and write opens a text file that allows you to read or append data at the end of the text (here, you also need to use the rewind () function to read the file, but you don't need the rewind () function to write the file, an is appended)

Append of ab binary data, if it does not exist, it can only be written.

Ab+ read and write opens a binary file, which is created if it does not exist, allowing you to read or append data at the end of the text. (for reading the file here, you also need to use the rewind () function, but writing to the file does not require the rewind () function. An is appended).

Thank you for your reading, the above is the content of "what is the file opening mode in C language". After the study of this article, I believe you have a deeper understanding of what the file opening mode is in C language. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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