LINUX Talk (Spend 10 minutes to learn blind box knowledge points) (perror, O_CREAT|O_RDWR, S_IRWXU, lseek, dup, system, struct stat statbuf, regular file bits)

ok friends, without further ado, let’s take a look at the code below!

First question:

if(fd1< 0) {
perror("open :");
printf("errno is:%d \
",errno);

This code is used to handle the situation where the file fails to open:

The

open() function will return a non-negative file descriptor when the file is successfully opened, and -1 if the opening fails.
Therefore, the if statement is used here to determine whether the file descriptor fd1 is less than 0. If it is less than 0, it means that the open failed.
The perror function can print out the error reason for the failure to open. It will print out the error string corresponding to the error code, such as “No such file or directory”.
At the same time, use printf to print out the specific error code errno, which is a global variable containing the system error number.
Different error numbers represent different error causes, and printing them can help locate the problem.

So overall, the function of this code is:
If the file opening fails (fd1<0), use perror to print the error reason string and the errno value to locate the specific reason for the opening failure. This is a common way to handle file operation errors.
Printing error messages can help debug and resolve why file opening fails.

Second question:

fd1=open("file1",O_CREAT|O_RDWR,S_IRWXU);

This code opens a file named file1 for reading and writing, and creates the file if it does not exist:

The open() function is a Linux system call used to open files
“file1” is the name of the file to be opened

What you need to know:
O_CREAT means create the file if it does not exist
O_RDWR means open the file for reading and writing
O_CREAT and O_RDWR are combined into one parameter through the bitwise OR | operation and passed to open()
S_IRWXU is a file permission constant. Its value 111 means that the file owner has read, write and execute permissions.
open() successfully returns a non-negative file descriptor fd1
fd1 can subsequently read and write the file, such as using read(), write() and other functions

So the function of this code is:
If file file1 exists, open it for reading and writing
If the file file1 does not exist, create the file and set the permissions to read, write and execute for the owner.
Returns the file descriptor fd1, which is used for subsequent file read and write operations through system calls.
Opening a file through the open() function and returning the file descriptor for I/O is a common file operation method in Linux programs.

Question 3:

lseek(fd2,16,SEEK_SET);
char buf[20]="ABCDEFGHIJKLMN";
fdw2=write(fd2,buf,20);

This code uses file descriptor fd2 to write data to the file:

The lseek() function is used to set/move the file read and write pointer position
fd2 is a valid file descriptor obtained through the open() function
SEEK_SET means calculating the position from the beginning of the file

lseek(fd2,16,SEEK_SET) locates the file read and write pointer to the 16th byte position at the beginning of the file
buf is a character array buffer containing 20 bytes of data “ABCDEFGHIJKLMN”
The write() function uses file descriptor fd2 to write the data in buf to the file.
It will start writing from the file pointer position, which is the 16th byte
The length of the written data is the length of buf, which is 20 bytes

So the function of this code is:
Use file descriptor fd2 to first locate the file pointer to a specific position (16th byte) through lseek(), and then use write() to write the data in the memory buffer to the file.
Random read and write access to files is realized by operating read and write pointers and data through file descriptors.

Question 4:

fd1 = dup(fd);
if(fd1 < 0)
perror("dup");

This code uses the dup function to copy the file descriptor fd:

– The dup function is used to copy an existing file descriptor and return a new file descriptor
– fd is a valid file descriptor obtained through the open function
– dup(fd) copies fd and returns the new file descriptor fd1
– If the return value fd1 of dup is less than 0, it means the copy failed.
– Then call the perror function to print the error message and inform the reason for the failure

The function of dup function is:

– Returns a new file descriptor for an open file
– Both the new and original file descriptors refer to the same underlying file
– Multiple file descriptors can be obtained for one file through dup
– These descriptors can be used for read and write operations on the same file

So this code uses the dup function to copy the file descriptor fd and obtain a new fd1. If it fails, print the error reason through perror.
Dup is very common in scenarios where you need to read and write a file at the same time.

Question 5:

system("rm -f file");

This code uses the system function to call the shell command rm to delete a file:

– The system function can call external commands (such as shell commands) in C programs
– rm is the file deletion command under Linux/Unix system
– The -f parameter indicates that no error message will be output even if the file does not exist.
– “rm -f file” is a shell command string used to delete a file named file
– The string is passed as a parameter to the system function
– The system function will start a subprocess and execute the shell command in the subprocess
– If the command is executed successfully, the system function returns 0, otherwise it returns a non-zero value

So the function of this code is:

Use the system function to call the rm command of the operating system to forcefully delete the file named file. If the file does not exist, no error will be reported.
Implementing file operations by calling external commands is simpler and more efficient than writing file deletion code yourself. This is a commonly used method in C language to execute external commands.

Question 6:

struct stat statbuf;

struct stat statbuf;
This line of code declares a variable statbuf of type structure (struct).

struct stat is a structure type defined by the Linux system, which is used to store file status information.
struct stat contains various attributes of the file, such as:
– Device No
– File types and modes
– Number of links
– User/Group ID
– File size
– Last access time
– Last Modified
– Last status change time
and other detailed file information.
By declaring this structure variable statbuf, you can subsequently use the stat system call to obtain the statbuf information of a certain file (through a file descriptor or path).
For example:
stat(“file”, & statbuf);
You can obtain the detailed status information of the file file and store it in the statbuf structure variable.
Then by accessing each member of statbuf, you can get various attribute values such as file size and type.
So this line of code declares a structure variable to store file status information. This is common in file operations.

Supplementary knowledge point: General file bits of file types

File Type Bits are stored in the st_mode field of the struct stat structure.
Under Linux systems, the file type bit is extracted using the file type mask defined by the S_IFMT macro:

#define S_IFMT 0170000
File type bits include:
– S_IFREG: Regular File, which contains data or code. 0x8000
– S_IFDIR: Directory,0x4000
– S_IFCHR: Character Device File, 0x2000
– S_IFBLK: Block Device File, 0x6000
– S_IFIFO: Pipe (FIFO/Named Pipe), 0x1000
– S_IFLNK: Symbolic Link, 0xa000
– S_IFSOCK: Socket, 0xc000
Among them, the value of S_IFREG 0x8000 represents the regular file identification in the file type bit.
By comparing with this value, you can use the S_ISREG macro to determine whether a file is a regular file type.
Therefore, in the Linux system, the specific value that identifies a regular file in the file type bit is 0x8000, which corresponds to the file type constant S_IFREG.
This can be used to identify file types after obtaining file information through stat.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 16952 people are learning the system