linux operation file

Everything is a file in Linux. Whether it is a file directory, a file, or a network device or a block device, they are all files.

 

 

system call

The system call is the interface provided by the operating system, and we can only deal with the operating system by using the system call. Let's take a look at the file-related system calls provided by the operating system.

  • Create a file

int create(const char *filename,mode_t mode);

The parameter mode represents the access authority of the newly created file, filename represents the string pointer to be passed in, and filename is the name of the file to be created.

  • open a file

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

The open() function has two forms, where pathname is the name of the file we want to open, and flags are several combinations of opening the file as shown in the figure below. Among them, only one of the three flags O_RDONLY, O_WRONLY, and O_RDWR can be used.

 If the O_CREATE flag is used, the function used is int open(const char *pathname, int flags, mode_t mode); at this time, we also need to specify the mode flag to indicate the access permission of the file.

  •  read and write files

After the file is opened, we can read and write the file. The system calls that provide file reading and writing in Linux are the read and write functions:

int read(int fd,const void *buf,size_t length);

int write(int fd,const void *buf,size_t length);

Among them, the parameter buf is a pointer to the buffer, and length is the size of the buffer. The function read() reads length characters from the file pointed to by the file descriptor fd into the buffer pointed to by buf, and returns the number of bytes actually read. The function write implements writing length characters from the buffer pointed to by buf to the file pointed to by the file descriptor fd, and returns the actual number of bytes written.

  • position

For random files, we can specify the location to read and write, and use the following function to locate:

int lseek(int fd,offset_t offset,int whence);

lseek() moves the file read/write pointer relative to whence by offset bytes. On success, returns the position of the file pointer relative to the file header.

SEEK_SET: relative file header

SEEK_CUR: Relative to the current article of the file read and write pointer

SEEK_END: ​​x relative to the end of the file

  • close file

When we have completed the operation, we need to close the file. At this time, we only need to call close, where fd is the file descriptor we want to close:

int close(int fd);

Next, we create a readable and writable file in the current directory, write "hello, sweet" in it, close the file, open the file again, read the content and output it to the screen.

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#define LENGTH 100
int main(){
    int fd,len;
    char str[LENGTH];
    
    fd=open("hello.txt",O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);//创建并打开文件

    if(fd){
        write(fd,"hello sweet",strlen("hello sweet"));
        close(fd);
    }

    fd=open("hello.txt",O_RDWR);
    len=read(fd,str,LENGTH);//读取文件内容
    str[len] = '\0';
    printf("%s\n",str);
    close(fd);
}

Execute the code and you can see the string.

hello sweet

Guess you like

Origin blog.csdn.net/weixin_42815827/article/details/128570985