Linux-file-open ()
Original article address:Introduction to Linux open Functions
Author:Xu
The open function is used to open and create files. The following is a brief description of the open function.
# Include<Fcntl. h>
Int open (const char * pathname, int Oflag ,...);
Returned value: if the operation is successful, the file descriptor is returned. Otherwise,-1 is returned.
For Open functions, the third parameter (...) is used only when a new file is created.Access limit(Access permission bits ). Pathname is the path name of the file to be opened/created (for example, C:/CPP/. CPP); Oflag is used to specify the file opening/creation mode. This parameter can be defined by the following constants (fcntl. h) by logic or composition.
O_rdonly read-only mode
O_wronly write-only mode
O_rdwr read/write mode
When opening/creating a file,At least one of the above three constants must be used.. The following constants are selected:
O_append each write operation writes the end Of the file
O_creat: if the specified file does not exist, create the file.
O_excl if the file to be created already exists,-1 is returned and the errno value is modified.
O_trunc if the file exists and is opened in write-only/read-write mode, all content of the file is cleared.
If the path name of o_noctty points to the terminal device, do not use this device as the control terminal.
O_nonblock if the path name points to a FIFO/block file/character file, set the file opening and subsequent I/ONon-Blocking Mode(Nonblocking Mode)
The following three constants are also selected for Synchronous input and output.
O_dsync waits for the physical I/O to end before writing. Without affecting the reading of newly written data, do not wait for the file attribute update.
O_rsync read waits until all write operations in the same region are completed.
O_sync waits for the physical I/O to end and then writes, including the I/O for updating the file attributes
The file descriptor returned by open must be the smallest unused descriptor.
If name_max (the maximum file name length, excluding '') is 14, and we want to create a file with a file name length of more than 14 bytes in the current directory, the early System V system (such as svr2) the excess part is truncated and only the first 14 bytes are retained. The BSD-derived (derived from BSD) system returns an error message and sets errno to enametoolong.
Posix.1Introduce Constants_ Posix_no_truncUsed to determine whether to intercept a long file name/long path name. If _ posix_no_trunc is setTruncation prohibitedAnd the path name length exceeds path_max (including ''), orArbitrary File NameIf the length exceeds name_max, an error message is returned and errno is set to enam1_long.
What is the difference
Fopen and open
Fread and read
Fwrite and write
Open and creat