Open functions in Linux and functions for exiting processes

Source: Internet
Author: User
Tags readable 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) Think : 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 open successfully, if the original file does not exist then create an empty new file, if the original file exists will re-create the file, the original content will be erased (a bit similar to deleting the original file before creating a new one)

(6) Is this likely to bring 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. We hope the effect is: if I creat to create a file that already exists in the name, then give me an error, do not create.

(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

1#include <stdio.h>2#include <sys/types.h>3#include <sys/stat.h>4#include <fcntl.h>5#include <unistd.h>6#include <string.h>7 8 9 Ten intMainintargcChar*argv[]) One { A     intFD =-1;//FD is the file descriptor, and the filename descriptor -     Charbuf[ -] = {0}; -     Charwritebuf[ -] ="l Love Linux"; the     intRET =-1; -      -     //First step: Open File -FD = open ("a.txt", O_RDWR | O_append |o_trunc); +     if(-1= = FD)//sometimes also written as: (FD < 0) -     { +printf"File open error \ n"); A         //return-1; at_exit (-1); -     } -     Else -     { -printf"file opened successfully, FD =%d.\n", FD); -     } in  - #if1 to     //Step Two: Read and write Files +     //Write a file -RET =Write (FD, WRITEBUF, strlen (WRITEBUF)); the     if(Ret <0) *     { $printf"write failed. \ n");Panax Notoginseng_exit (-1); -     } the     Else +     { Aprintf"Write succeeded,%d characters were written \ n", ret); the     } + #endif -  $  $ #if0 -     //Read the file -ret = Read (FD, BUF,5); the     if(Ret <0) -     {Wuyiprintf"read failed \ n"); the_exit (-1); -     } Wu     Else -     { Aboutprintf"the actual read of%d bytes. \ n", ret); $printf"The file content is: [%s].\n", buf); -     } - #endif     -  A     //Step Three: Close the file + Close (FD); the      -_exit (0); $}

Open functions in Linux and functions for exiting processes

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.