Analysis of File open mode (r/w/a/r+/w+/a+/rb/wb/ab/rb+/wb+/ab+) in C language

Source: Internet
Author: User

Analysis of open mode of C language file

In the C language file manipulation syntax, open file files have the following 12 modes, such as:

open Mode

  can only read  

  can only write  

read Write both

text mode

R

W

a

r+

w+

A +

binary mode

RB

WB

AB

 rb+ (r+b)  

 wb+ (w+b)  

 ab+ (a+b)  

The binary mode is similar to the text mode operation, except that it is read and write in the form of a binary stream, the following is an example of text mode :

1."R" mode :

1.1 Open file for " Read Only " operation, that is, the content can only be read from the file.

1.2 If the file you want to manipulate does not exist, the open fails .

1.3 When the file is successfully opened, the file pointer is at the beginning of the file.

1.4 When you open a file, the original content in the file is not emptied .

1.5 The content can be read from anywhere in the file.

2."W" mode :

2.1 Open file for " write only " operation, that is, the content can only be written to the file.

2.2 If the file you want to manipulate does not exist, create a new file .

2.3 When the file is successfully opened, the file pointer is at the beginning of the file.

2.4 When the file is opened, the original contents of the file are emptied .

2.5 can write to any location in the file, and when the write operation, the contents of the original location will be overwritten .

3."A" mode :

3.1 Open the file for an " append " operation, that is, you can only write to the file.

3.2 If the file you want to manipulate does not exist, create a new file .

3.3 When the file is successfully opened, the file pointer is at the end of the file.

3.4 When you open a file, the original content in the file is not emptied .

3.5 can append (write) content to the end of the file only.

4."r+" mode :

4.1 Open the file for " Read and write " operation, that is both readable and writable.

4.2 If the file you want to manipulate does not exist, the open fails .

4.3 When the file is successfully opened, the file pointer is at the beginning of the file.

4.4 When you open a file, the original content in the empty text is not cleared .

4.5 Whether the content is read or written, can be anywhere in the file, and write operations, will overwrite the original location of the content.

5."w+" mode :

5.1 Open the file for " Read and write " operation, that is both readable and writable.

5.2 If the file you want to manipulate does not exist, create a new file .

5.3 When the file is successfully opened, the file pointer is at the beginning of the file.

5.4 When the file is opened, the original contents of the file are emptied .

5.5 Whether the content is read or written, can be anywhere in the file, and write operations, will overwrite the original location of the content.

6."A +" mode :

6.1 Open the file for " Read and write " operation, that is both readable and writable.

6.2 If the file you want to manipulate does not exist, create a new file .

6.3 When the file is successfully opened, the file pointer is at the end of the file.

6.4 When you open a file, the original content in the file is not emptied .

6.5 When the content is read, it can be done anywhere , but when the content is written, it is appended only to the end of the file.

Attached form to strengthen memory:

Open mode

Briefly

If you want to operate the

File does not exist

After the file is successfully opened

File pointer location

Whether to empty

Original content

Read location

Write location

Note

R

Read-only

Open failed

Beginning

Whether

Any position read

Not writable

-

W

Write only

New

Beginning

Is

Non-readable

Write Anywhere

Overwrite original location content when writing

a

new

end

no

cannot read

   

r+

Write

Open failed

Beginning

Whether

Any position read

Write Anywhere

Overwrite original location content when writing

w+

new

Start

is

  anywhere read  

  write   anywhere,

  overwrites the original location content  

A +

New

End

Whether

Any position read

only Tail Write

-

Different open modes correspond to different operating modes and need to be carefully planned for use.

Analysis of File open mode (r/w/a/r+/w+/a+/rb/wb/ab/rb+/wb+/ab+) in C language

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.