Linux-open function

Source: Internet
Author: User
Tags terminates

The flag of the Open function is detailed 1

Read and Write permissions: O_rdonly o_wronly o_rdwr

(1) Linux Chinese parts have read and write permissions, we can also open the file with a certain permission description (for example, O_rdonly is opened as read-only, o_wronly means to open in a write-only way, o_rdwr means to open in a readable and writable way)

(2) When the permission is attached, the open file can only be manipulated with this permission.

When you open a file that exists and has content: O_append, O_trunc

(1) Consider a question: what happens when we open a file that already exists and has content inside it?

Possible result 1: New content will replace the original content (the original content is gone, lost)

Possible result 2: New content added in front, the original content continues in the back

Possible result 3: New content attached to the back, the original content is still in front

Possible result 4: The contents of the original file remain unchanged when not read and not written

(2) O_trunc property to open the file, the original content will be discarded if it is originally content. This corresponds to the above result 1

(3) O_append property to open the file, if the file is originally content, then the newly written content will be followed by the original content, corresponding to the result 3

(4) Default not using the O_append and O_trunc properties is the result 4

(5) What if O_append and O_trunc appear at the same time?

Exit, _exit, _exit quit process

(1) When our program in the previous steps failed to cause the subsequent operation is not possible to go on, should be in the previous error monitoring to end the entire program, should not continue to let the program run.

(2) How do we exit the program?

First, in main with return, the general principle is that the program terminates return 0 normally, if the program terminates abnormally return-1.

Second: The formal Termination process (program) should use Exit or one of _exit or _exit.

The flag of the Open function is detailed 2

When opening a nonexistent file: O_creat, O_EXCL

(1) Thinking: What happens when we go to open a file that doesn't exist? When we open a file, if the file name does not exist, it will open a file error.

(2) vi or Windows notepad++, you can directly open a file that does not exist.

(3) The open flag O_creat is to deal with the opening of a file that does not exist. O_creat means that the file we are currently opening does not exist, and we are going to create it and open it.

(4) Thinking: What happens when we open using O_creat, but the file already exists? Experimental results show that the result is an error.

(5) Conclusion: After adding o_creat in open, regardless of whether the original file exists or not can be opened successfully, if the original file does not exist then create an empty new file, if the original file exists will recreate the file, The original content will be erased (a bit similar to deleting the original file before creating a new one)

(6) This could lead to a problem? We originally wanted to create a new file, but the file name was mistaken for an old file name, the result of the old files were accidentally modified. The effect we want is: if I creat to create a file with a name that already exists, give me an error and don't create it.

(7) This effect depends on the O_EXCL logo and the O_creat logo to be used together. When this is a symbol, when there is no file to create the file, there will be an error in this file to remind us.

(8) When using the O_CREAT flag to create a file, the Open function can use the third parameter, mode, to specify the permissions of the file to be created. Mode uses 4 numbers to specify permissions, and the next three are important, corresponding to the permission flags of the file we are creating. For example, it is common to create a readable writable non-executable file with 0666

Linux-open function

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.