Linux Basic Tutorial: 7. File I/O Operation Supplement

In the previous issue, we introduced the reading (read), writing (write) and closing (close) of files. In this issue, we are expanding several functions:

1. lseek function

This function is closely related to our mouse cursor. It can set the starting position of the cursor, which means that this function can determine the starting position of file copying and writing. Similarly, you can use man 2 lseek to view The specific information of this function, we will not go into details here, we will directly mention a few important points:

  //设置复制起始点
int indexr=lseek(fb1,5,SEEK_SET);
int indexw=lseek(fb2,4,SEEK_SET);

The first parameter of this function is the file descriptor, which is the return value of the open mentioned above, which represents the file we want to move the cursor to. The second parameter is the length of the movement. If it is negative, it will move forward, and if it is positive, it will be In the future, the third is the initial position of the cursor. There are three options: SEEK_SET, SEEK_END, SEEK_CUR. In fact, you can know the general meaning by looking at it. The first is to set the position of the cursor, and the second is to move from the end of the file. , that is, the cursor will go to the end of the file, and the third one is to move from the current position of the cursor; the return value is the number of bytes in front of the cursor, so we can use lseek to calculate the size of the file:

 int content=lseek(fb2,0,SEEK_END);

You can see that the number printed here is the size of the written file, and this function is also in the unistd.h header file, so you should pay attention to whether you have included it when using it;

lseek also has a function that can change the size of the file, by inserting some " ", and moving the cursor:

lseek(fb2,100,SEEK_CUR);
write(fb2," ",1);

 

Move the cursor through this function, move 100 units backward from the current position, and then insert a space. The size of the file before was 2365. If we execute the program, it will become 2465:

It can be seen that it is obviously expanded by about 100 units, there may be some errors, but it is harmless;

2. stat function

This function can get the attributes of the file, it is in the sys/stat.h file, we can use it to get the file attributes:

struct stat sbuf; 
result=stat("readtext.txt",&sbuf);

This function has two parameters, the first is the name of the file, and the second parameter is to pass a structure. Of course, this structure is defined by ourselves, but after the function is successfully executed, it will have the following properties. If the execution fails will return -1:

 struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* Inode number */
               mode_t    st_mode;        /* File type and mode */
               nlink_t   st_nlink;       /* Number of hard links */
               uid_t     st_uid;         /* User ID of owner */
               gid_t     st_gid;         /* Group ID of owner */
               dev_t     st_rdev;        /* Device ID (if special file) */
               off_t     st_size;        /* Total size, in bytes */
               blksize_t st_blksize;     /* Block size for filesystem I/O */
               blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */
​
               /* Since Linux 2.6, the kernel supports nanosecond
                  precision for the following timestamp fields.
                  For the details before Linux 2.6, see NOTES. */
​
               struct timespec st_atim;  /* Time of last access */
               struct timespec st_mtim;  /* Time of last modification */
               struct timespec st_ctim;  /* Time of last status change */
​
           #define st_atime st_atim.tv_sec      /* Backward compatibility */
           #define st_mtime st_mtim.tv_sec
           #define st_ctime st_ctim.tv_sec
           };

So we can write a program to see if the properties of the file are correctly obtained:

#include<stdio.h>
#include<sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include<stdlib.h>
int main()
{
​
        int fd1,result=0;
        struct stat sbuf;
​
        result=stat("readtext.txt",&sbuf);
        if(result==-1)
        {
                perror("stat error");
                exit(1);
        }
        printf("len=%1d\n",sbuf.st_size);
        return 0;
}

 

After executing the program, the value of the attribute is indeed obtained, and it is also correct. Here we obtain the length of the readtext.txt file;

3. access function

This function is used to test whether the file has certain permissions. This function is also in the unisted.h file and has two parameters:

result=access("readtext.txt",R_OK);
if(result!=-1){
    printf("文件是可以阅读的\n");
}
else
{
    printf("文件是不可以阅读的\n");
}
​

The first parameter is the path name of the file, the second is the permission, here are three options: R_OK, W_OK, F_OK, they respectively represent the readable, writable, and executable of the file, here we make a judgment Executable permissions, there is no problem after execution:

 

4. chmod function

This function is used to modify the access rights of the file. This function is also in the sys/stat.h file. It also has two parameters. The first parameter is the file path name, and the second is the permission to be modified. Here we pass It is an octal number. We said before that the file permission is an octal number:

 

You can see that the permissions of our readtext files here are rw-rr, and we try to change them to rwx-rwx-rwx:

int result=0;
result=chmod("readtext.txt",0777);
if(result==-1)
{
printf("this is a error\n");
​
}
else
{
printf("sccessud\n");
​
}

We now write our file name on the first parameter of chmod, and then the second parameter is the permission we want to change, here 7 is the corresponding rwx, and it also has a return value, if the call function fails will return a -1:

Compile and run to see that our program runs successfully, and then look at the permissions:

Obviously, our permissions here are indeed modified successfully;

5. The truncate function

truncate is used to modify the size of the file, lseek can also modify the size, but this only needs to be called once, and lseek also needs to write " ", the operation of moving the cursor, but their effects are similar, This function also has two parameters, the first is the path name of the file, and the second is the length, which is the length we want to set for the file. It also has a return value. If the call fails, it will return -1:

 int result=0;
 result=truncate("readtext.txt",3000);
 if(result==-1)
 {
 printf("error");
 }
 else
 {
​
printf("sucessd");
}
​

We are setting the size of the readtext file to 3000, the original size is 2357:

Let's execute the program and look at it again:

 

 You can see that this file has been modified to 3000;

Guess you like

Origin blog.csdn.net/aiwanchengxu/article/details/127666990