Linux day3 file system

1. Linux file system

1. Create

int creat(const char *filename,mode_t mode)

The parameter mode is the permission to store the file

2. Open

int open(const char*pathname,int flag)
int open(const char*pathname,int flag,mode_t mode)

flag file open flags:

O_RDONLY: open the file as read-only

O_WRONLY: open the file as write-only

O_RDWR: open file for reading and writing

O_APPEND: open the file in append mode

O_CREAT: create a file

O_EXEC: An error occurs if O_CREAT is used and the file already exists

O_NOBLOCK: open a file in a non-blocking manner

O_TRUNC: If the file already exists, delete the file content

If the O_CREAT flag is used, the function used is int open(const char*pathname, int flag, mode_t mode). At this time, the mode flag should be specified to indicate the access permission of the file.

mode file access permissions

S_IRUSR User can read

S_IWUSR user can write

S_IXUSR user can execute

S_IRWXU user can read, write, execute

S_IRGRP group can be read

S_IWGRP group can write

S_IXGRP group can read, write, execute

S_IROTH others can read

S_IWOTH others can write

S_IXOTH others can execute

S_ITWXO Others can read, write and execute

S_ISUID sets the user execution ID

S_ISGID sets the group execution ID

3. Read and write

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

Can only read and write after the file is opened

4. Positioning

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

lseek() moves the file read/write pointer by offset bytes relative to whence. When the operation is successful, it returns the position of the file pointer relative to the file header. The parameter whence can use the following values:

SEEK_SET: Relative file header file

SEEK_CUR: The current position of the relative file read and write pointer

SEEK_END: ​​Relative to end of file

offset can take a negative value

lseek(fd,0,SEEK_END) length of the return value of the file

5. Close

int close(int fd)

Second, the C library file system

1. Create and open

FILE *fopen(const char *path,const char *mode )

2. Read and write

int fgetc(File *stream ) 
int fputc(int c,FILE *steam) 
char *fgets(char *s,int n,FILE *steam) 
int fputs(const char *s,FILE *stream) 
int fprintf(FILE *stream,const char *format,...) 
int fscanf(FILE *steam,const char *format,...) 
size_t fread(void *ptr,size_t size ,size_t n,FILE *stream) 
size_t fwrite(const void *ptr ,size_t size ,size n,FILE *stream)

3. Close

int fclose(FILE *stream)


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324646332&siteId=291194637