1. Create a file:
Create files can be called by the Linux system with the prototype: int creat (const char*filename,mode_t mode)
FileName: The name of the file you want to create (contains the path, default is to create the file under the current path)
mode_t: Create a schema that represents the permissions for the file you created.
S_IRUSR readable
S_IWUSR can write
S_IXUSR Executable
S_irwxu Readable writable Executable
It can also be represented directly by numbers, such as when mode_t takes 0666 to indicate that the file created is readable and writable for both the file owner and the group where the file owner resides, and for other people.
Note: The actual seemingly rarely used creat system call to create a file, because the system calls open not only has the function of opening a file, but also has the ability to create a file.
2. Open (Create) file:
Opening or creating a file can use the system to call Open, which has two forms of prototype:
First form (pure Open file): int open (const char*pathname,int flags)
Second form (with the ability to open files and create files): int open (const char*pathname,int flags,mode_t mode)
Pathname: You need to open the file name, including the path, by default to open the file under the current path.
Flags: Open Flag
O_rdonly open file with read-only mode
O_wronly to open a file in a write-only manner
O_RDWR Read and write open a file
O_append Append method to open the file, open the file when the file pointer to the end of the open file, add the content to the end of the file
o_creat If the file you want to open does not exist, create a new file and use the third parameter mode_t to specify permissions for the created file
O_noblock open file in non-blocking mode
Opening a file with the open system call returns a file descriptor FD, which is a nonnegative integer, each open file corresponds to the only FD, you can interpret the FD as the * * * Number of the file being opened, and then use this FD to perform other operations on the file.
When the open call succeeds, it returns the file descriptor of the opened file, which returns 1 on failure.
Note: Using the open system call needs to include the following header files:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
3. Close the file:
When you do not need to do something else with the open file, we need to close the file using close, which is the prototype of the close system call:
int close (int fd), where FD is a file descriptor
The close call returns 0 on success, and 1 if it fails.
Note: Use the close system call to include header files:
#include <unistd.h>
4. Read the file:
Read the file contents using the Read system call, whose prototype is int read (int fd,const void*buf,size_t length)
FD: File descriptor indicating which file you want to read
BUF: Buffer, the content after reading the file is placed in this buffer
Length: The number of bytes you want to read
function function: Reads the length bytes from the file descriptor fd to the buffer buf, and then returns the actual number of bytes read from the file after read, or 0 or -1,0 if read fails, which means that the read pointer to the file has reached the end of the file and the content cannot be read from the file. -1 indicates a read error.
Use the read system call to include the header file:
#include <unistd.h>
5. Write the file:
Write the file using the write system call, whose prototype is int write (int fd,const void*buf,size_t length)
FD: A file descriptor that indicates which file you want to write to.
BUF: Buffer, you write the data source of the written file, that is, the data you want to write into a file is taken from this buffer.
Length: The number of bytes written to the file.
function function: Take length bytes of data from the BUF buffer and write the data into the corresponding file of FD, then return the number of bytes actually written to the file after write, and return 1 if write fails.
Use the write system to call the header file that should be included:
#include <unistd.h>
6. Position the location of the file pointer:
When we read and write to a file using read or write, we start with Read and write by default from the current file pointer, so let's say I'm going to start reading or writing at the last 100 bytes of a file, and you have to move the pointer to the specified location. Before it can be read or written correctly.
The location of the position file pointer is called with the Lseek system, and its prototype is:
int lseek (int fd,offset_t offset,int whence)
FD: A file descriptor that indicates which file you want to position and move the pointer to.
Offset: The distance the file pointer moves relative to the original position, which is the offset.
Whence: The original position of the file pointer, which is the datum point where you want the file pointer to move.
function function: Move the pointer of the file relative to the datum point whence the offset byte, if the Lseek succeeds returns the position of the file pointer relative to the file header, if it fails, returns-1.
Use the Lseek system call to include the header file:
#include <unistd.h>
#include <sys/types.h>
7. File Access judgment:
Sometimes we need to determine whether a file can do something (read or write, etc.), and then you can use the system to call access with the following prototype:
int access (const char*pathname,int mode)
Pathname: The file name (containing the path, the default is the current path), indicates which file you want to judge.
Mode: To determine the access rights.
R_OK file readable
W_OK file can be written
X_OK File Executable
F_OK file exists
When the test succeeds, returns 0, otherwise returns-1.
Use the access system to call the header files that you want to include:
#include <unistd.h>
This article is from the "Stop Thinking" blog, make sure to keep this source http://9110091.blog.51cto.com/9100091/1605159
Linux System Programming Note: File programming