Notes on C and pointer-do not underestimate the mode parameter of fopen

Source: Internet
Author: User

R: open a read-only file, which must exist.

R + has read/write attributes. It is written from the file header and retains the content not covered in the original file.This file must exist.


W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created.
W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created.


A. Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.
A+ Open readable and writable files by appending them. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.

A and A + are described as follows:
"A" mode does not clear the EOF flag before adding the data. After adding the data, use the Type command of the MS-DOS to only display the data to the original EOF flag without displaying the later added.
The "A +" Mode clears the EOF flag and displays all the data with the Type command of the MS-DOS after adding the data.In the "A +" mode, you need to enter the stream that ends with the CTRL + z EOF sign.

However, I tried to write the same characters using a and a + in Linux, and there is no difference in EOF:
For example, if the content of the original file is ABC, if the mode is used to add 123, then read the file, stop the output when the EOF mode is encountered, and finally show ABC123, if according to the above statement, only ABC is displayed.
If the + mode is used, the last displayed value is abc123.
Therefore, the above statement is not true in Linux and may be used in windows.

Finally, the description in man fopen in Linux:

A open for appending (writing at end of file). The file is created if it does not exist.
Stream is positioned at the end of the file.

A + open for reading and appending (writing at end of file). The file is created if it does not
Exist. The initial file position for reading is at the beginning of the file, but output is
Always appended to the end of the file.

The EOF problem is not mentioned above.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.