File Operations in Linux

Source: Internet
Author: User
File Operations in Linux-general Linux technology-Linux programming and kernel information. For details, refer to the following section. Preface:
In this section, we will discuss various functions for file operations in linux.
File Creation and read/write
File Attributes
Directory file operations
MPs queue File

--------------------------------------------------------------------------------
1. File Creation and read/write
I suppose you already know all the functions for standard file operations (fopen, fread, fwrite, etc ). of course, don't worry if you don't know. the system-level file operations we discuss actually serve standard-level file operations.
When we need to open a file for read/write operations, we can use the system call function open. After the use is complete, we call another close function to close the operation.
# Include
# Include
# Include
# Include

Int open (const char * pathname, int flags );
Int open (const char * pathname, int flags, mode_t mode );

Int close (int fd );

The open function has two forms. here, pathname is the name of the file we want to open (including the path name, which is considered to be under the current path by default ). flags can be used to combine the following values or values.
O_RDONLY: open the file in read-only mode.
O_WRONLY: open the file in write-only mode.
O_RDWR: open a file in read/write mode.
O_APPEND: open the file in append mode.
O_CREAT: Creates a file.
O_EXEC: If O_CREAT is used and the file already exists, an error occurs.
O_NOBLOCK: open a file in non-blocking mode.
O_TRUNC: if the file already exists, delete the file content.
Only one of the first three labels can be used. if the O_CREATE flag is used, we need to use the second form of open. specify the mode flag to indicate the object access permission. mode can be a combination of the following conditions.
-----------------------------------------------------------------
S_IRUSR users can read S_IWUSR users can write
S_IXUSR user can execute S_IRWXU user can read and write execution
-----------------------------------------------------------------
S_IRGRP group can read S_IWGRP group can write
S_IXGRP group can run S_IRWXG group can read and write
-----------------------------------------------------------------
S_IROTH others can read S_IWOTH others can write
S_IXOTH others can execute S_IRWXO others can read and write
-----------------------------------------------------------------
S_ISUID: Set the user execution ID S_ISGID: Set the execution ID of the Group
-----------------------------------------------------------------
We can also use numbers to represent the symbols of each bit. Linux uses a total of five numbers to represent various permissions of the file.
00000. the first digit indicates setting the user ID. The second digit indicates setting the group ID, the third digit indicates the user's own permission, the fourth digit indicates the Group permission, and the last digit indicates the permissions of others.
Each number can be 1 (Execution permission), 2 (write permission), 4 (read permission), 0 (nothing), or the sum of these values.
For example, if you want to create a user read/write execution, the group does not have the permission to read the files executed by others. set the user ID. The available mode is -- 1 (set the user ID) 0 (the group is not set) 7 (1 + 2 + 4) 0 (no permission, use the default value) 5 (1 + 4) is 10705:
Open ("temp", O_CREAT, 10705 );
If the file is successfully opened, open will return a file descriptor. We will be able to operate all operations on the file descriptor in the future.
After the operation is complete, we need to close the file. You only need to call close. fd is the file descriptor to be closed.
After the file is opened, we need to read and write the file. We can call the read and write Functions to read and write the file.
# Include

Ssize_t read (int fd, void * buffer, size_t count );
Ssize_t write (int fd, const void * buffer, size_t count );

Fd is the file descriptor for reading and writing operations, and buffer is the memory address for writing or reading the file content. count is the number of bytes to read and write.
For a Common File read, read the count byte from the specified file (fd) to the buffer (remember that we must provide a buffer that is large enough), and return the count.
If the read reads the end Of the file or is interrupted by a signal, the returned value is smaller than count. if it is returned due to a signal interruption and no data is returned, read returns-1 and errno is set to EINTR. when the program reads the end of the file, read returns 0.
Write writes the count byte from the buffer to the file fd. The actual number of bytes written is returned when the operation is successful.
Next we will learn an instance which is used to copy files.

# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define BUFFER_SIZE 1024

Int main (int argc, char ** argv)

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.