2.1. File read and Write permission
(1) Linux Chinese parts have read and write permission, we open the file in open can also be accompanied by a certain permission to explain (for example, O_rdonly is read-only open, o_wronly means open in a write-only way, o_rdwr means to open in a readable and writable way) When we have some kind of permission attached to the open file, the open file can only be manipulated by that permission.
2.2. Change the contents of the file
(1) When we open the existing and internal content of the file will appear in 4 cases (the original content disappeared (using the O_TRUNC flag), the new content is added after the original content (using the O_append flag), new content is added before the original content , the original contents of the file are not changed when the file is not read and written (the default is not to use the o_append and O_trunc flags), and if O_append and O_trunc are present at the same time, the O_TRUNC flag function is blocked by code validation o_append flag.
2.3. Exit process or procedure
(1) When the program (process) in the previous operation failed to carry out the subsequent operation is not possible, we should be in the previous error monitoring program to end the entire program (process), should not let the program (process) continue to run.
(2) in the main function of the return keyword, the general principle is the normal termination of the program return 0, the program abnormally terminated return-1, not commonly used.
(3) The formal termination of the process (program) should use Exit or _exit or _exit one, the general principle is that the process normally terminates exit (0), the program terminated exit (-1), often used.
2.4. Open files that do not exist
(1) When we open a file for the existence of the file there will be 2 scenarios (create and open files that do not exist, if the file exists, the file will be modified to reset (using the O_CREAT flag), create and open a file that does not exist, and if the file exists, it will have an error. Modifying the file (using both the O_EXCL flag and the O_CREAT flag) is not created.
(2) When you use the O_CREAT flag to create a file, the Open function can use the 3rd argument mode to specify the permissions of the file you want to create; mode uses 4 digits to specify permissions, with the following 3 important, which corresponds to the permission flags for the target file we are creating , for example, to create 1 readable writable files that are not executable by 0666.
2.5. Blocking and Non-blocking
(1) If a function is blocked, then the current process may be blocked when we call the function (in essence, the condition of the function to be finished is not available, cannot be done at present, wait for the condition to be ripe), the function is blocked and cannot be returned immediately; If a function is non-blocking, Then we must return immediately after calling this function, but the function does not necessarily complete the task.
(2) Blocking and non-blocking are two different design ideas, and there is no good or bad; In general, blocking results are guaranteed but time is not guaranteed; non-blocking time is guaranteed but results are not guaranteed.
(3) The API provided by the operating system and the library functions encapsulated by the API, many of which are designed to be blocked or non-blocking in themselves, so when our application calls these functions it must be clear whether the function is blocking or non-blocking.
(4) By default, we open a file is blocked (if there is a problem when reading and writing to the file when the file will cause blocking), if you want to open the file in a non-blocking way, then flag to add the O_NONBLOCK flag Blocking and non-blocking are used only for device files (Linux hardware devices such as serial ports, I2C communications devices, LCD) and not for normal files.
2.6. Bottom block and non-blocking
(1) Write by default only to write content to the underlying buffer to return, and then the bottom (operating system in charge of the implementation of open, write these operations of the Code, also contains the OS read hard disk, such as the underlying hardware code) in the appropriate time automatically will be the contents of the underlying buffer one-time synchronization to The design mechanism is to improve the hardware operation performance and hardware life; but sometimes we want the hardware not to wait and write our content directly to the hard disk, then use the O_SYNC flag (write blocking waits for the underlying finish write before returning to the application tier).
2.open_exit * * Company: XXXX * Author: Rston * Blog: Http://blog.csdn.net/rston * Github:https://github.com/rston * PROJECT: FLA of open function
G Detailed * Function: file read and Write permissions, change the contents of the file, exit the process or program, open the file does not exist. * #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include & lt;unistd.h> #include <string.h> #include <stdlib.h> int main (int argc, char **argv) {int FD =-1; FD is file descriptor, the filename descriptor int ret =-1; Used to receive the read return value to determine if the file succeeded char buf[100] = {0}; The build buffer holds the content read from the file char bufwrite[20] = "Linux is great"; The build buffer holds the content to write to the file//open Test.txt file, if the file does not exist to create (permission 666), if the file exists error FD = open ("Test.txt", O_RDWR | O_creat |
O_EXCL, 0666);
if ( -1 = FD)//To determine whether the file was opened successfully, you can also write if (FD < 0) {printf ("Open File error.\n"); return-1;
Use return or exit to exit a process or program Exit (-1); else {printf ("Open file sucess.
FD =%d.\n ", FD); #if 1 ret = write (FD, Bufwrite, strlen (Bufwrite)); Write content to file if (Ret < 0)//Determine if content was successfully written to file {printf ("Write File errot.\
n ");
return-1;
Exit (-1);
else {printf ("write%d bytes to file.\n", ret);
printf ("The content of write is [%s].\n", bufwrite); #endif #if 1 ret = read (FD, buf, sizeof (BUF));
Read the contents of the file if (Ret < 0)//judge whether the read file was successful {printf ("read file error.\n");
return-1;
Exit (-1);
else {printf ("read%d bytes actual from file.\n", ret);
printf ("The content of Read is [%s].\n", buf); #endif Close (FD);
Close file//return 0;
Exit (0);
}