Home >Operation and Maintenance >Linux Operation and Maintenance >Linux file operations
The example of this article describes the method of interactive execution of python file reading and writing operations and Linux shell variable commands. Share it with everyone for your reference. The details are as follows:
Related system calls for file operations
Create
int creat(const char *filename, mode_t mode);
Parameter mode specifies new The access permission of the file, which together with umask determines the final permission of the file (mode & umask), where umask represents some access permissions that need to be removed when the file is created. It only affects read, write and execution permissions. The calling function is int umask (int newmask).
Open
int open(const char *pathname, int flags);
pathname is the file name we want to open (including the path name, the default is the current path)
flags Open flag
O_RDONLY Open the file in read-only mode
O_WRONLY Open the file for writing only
O_RDWR Open the file for reading and writing Open the file as
O_APPEND Open the file as append
O_CREAT Create a file
When flag is O_CREATE, specify the mode flag to indicate file access permissions
##S_IRWXU User can read, write and execute
##S_IRGRP group can read
S_IWGRP group can write
S_IXGRP group can execute
The S_IRWXG group can read, write, and execute
S_IROTH Others can read
Each number can take the value of 1 (execution permission), 2 (write permission), 4 (read permission), 0 (none), or the sum of these values.
The first digit indicates setting the user ID
open("test", O_CREAT , S_IRWXU | S_IROTH | S_IXOTH | S_ISUID );
Read and write
int read(int fd, const void *buf, size_t length);
read() reads length bytes from the file specified by the file descriptor fd into the buffer pointed by buf. The return value is the actually read bytes. Number
Positioning
SEEK_SET: relative to the beginning of the file.
SEEK_CUR: The current position of the relative file read and write pointer.
Close
int close(int fd);
File operation of C library function-independent of the specific operating system platform
Create and open
FILE *fopen(const char *path, const char *mode);
fopen() realizes opening the specified file filename, where the mode is the open mode, Linux The system does not distinguish between binary files and text files. The value of mode
r, rb is opened in read-only mode
a, ab are opened in append mode. If the file does not exist, create the file
int fputc(int c, FILE *stream);
char *fgets(char *s, int n, FILE *stream);
int fputs(const char *s, FILE *stream);
int fprintf(FILE * stream, const char *format, ...);
int fscanf (FILE *stream, 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_t n, FILE *stream);
int fsetpos(FILE *stream, fpos_t *pos);
nt fsetpos(FILE *stream, const fpos_t *pos);
int fseek(FILE *stream, long offset, int whence);
An intuitive reflection of the kernel device tree. When a kernel object is created, the corresponding files and directories are also created in the kernel object subsystem.
linux Parent directory permissions affect subdirectory files Operation
How to interactively execute python file read and write operations with linux shell variable commands
The above is the detailed content of Linux file operations. For more information, please follow other related articles on the PHP Chinese website!