1. Create
int creat (constchar *filename, mode_t mode);
Parameter mode specifies access to the file, mode together with Umask determines the final permission (mode&umask) of the file, and Umask represents some of the access permissions that the file needs to be removed when it is created.
Umask can be changed by system call Umask ():
int umask (int newmask);
The call sets Umask to Newmask, and the function returns the old Umask permission, which affects only the read, write, and execute permissions.
2. Open
int Open (constcharint flags); int Open (constcharint , flags, mode_t mode);
The open () function has two ways, where pathname is the file path name and defaults to the current path.
The flags indicate the following:
O_rdonly, O_wronly, O_RDWR Three signs can only use one of them,
If the O_CREAT flag is used, the function must specify that mode is used to represent the access rights of the file.
int Open (constchar *pathname, o_creat, mode_t mode);
Mode is indicated as follows:
3. Reading and writing
After the file open we return an FD (file descriptor) that we can read and write through the file descriptor.
int Read (intconstvoid *buf, size_t length); int write (intconstvoid *buf, size_t length);
Where BUF is a pointer to a buffer, length is the size of the buffer in bytes.
The function read () Implementation reads the length bytes from the file specified by the file descriptor FD to the buffer pointed to by BUF, and the return value is the number of bytes actually read.
The function write () implementation writes a length of bytes from the buffer pointed to by BUF to the file that the file descriptor is pointing to, and the return value is the number of bytes actually written.
4. Positioning
For random files, we can randomly specify the location to read and write, you can use the following function to locate:
int lseek (intint whence);
Lseek () Moves the file read-write pointer to offset bytes relative to whence. The operation succeeds, returning the position of the file pointer relative to the file header.
The parameter whence can use the following values:
Seek_set: Relative file start
Seek_cur: The current position of the relative file read-write pointer
Seek_end: Relative file end
Offset can have a negative value, and the Lseek (fd,-5,seek_cur) file pointer moves forward by 5 bytes relative to the current position.
Since the Lseek function returns the byte length of the beginning of the file to the current pointer, Lseek (FD, 0, seek_end) returns the entire file length.
5. Close
int close (int fd);
File Operations for Linux