In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you the example analysis of Linux file operation, I believe most people still do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
create
int creat(const char *filename, mode_t mode);
The parameter mode specifies the access permissions of the new file, which together with umask determines the final permissions 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 execute permissions, and the call function is int umask(int newmask).
open
int open(const char *pathname, int flags);
pathname is the name of the file we want to open (including the path name, default under the current path)
flags Open flags
O_RDONLY opens files read-only
O_WRONLY Open file in write-only mode
O_RDWR Open file read-write
O_APPEND Open file by appending
O_CREAT creates a file
O_EXEC If O_CREAT is used and the file already exists, an error occurs
O_NOBLOCK opens a file in a non-blocking manner
O_TRUNC If the file already exists, delete its contents
int open(const char *pathname,int flag,mode_t mode)
When flag is O_CREATE, specifies the mode flag used to indicate file access rights
S_IRUSR users can read
S_IWUSR Users can write
S_IXUSR user can execute
S_IRWXU Users can read, write and execute
S_IRGRP group can be read
S_IWGRP group can be written
S_IXGRP group can execute
S_IRWXG group can read, write and execute
S_IROTH Others can read
S_IWOTH Others can write
S_IXOTH Others can perform
S_IRWXO Others can read, write and execute
S_ISUID Sets the user's execution ID
S_ISGID Execution ID of Settings Group
The mode flag can also be used to indicate file permissions numerically:
Each number can take 1 (execute permission), 2 (write permission), 4 (read permission), 0 (none), or the sum of these values.
The first digit indicates the setting user ID
The second digit indicates the setting group ID
The third digit represents the user's own permission bit
The fourth digit indicates group permissions
The fifth digit indicates the authority of others.
open("test", O_CREAT, 10705);
The above statement is equivalent to:
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);int write(int fd, const void *buf, size_t length);
Parameter fd File descriptor, buf is a pointer to the buffer, length is the size of the buffer in bytes, and the return value is the actual number of bytes read and written.
read( ) reads length bytes from the file specified by the file descriptor fd into the buffer pointed to by buf, and the return value is the actual number of bytes read
The write( ) implementation writes length bytes from the buffer pointed to by buf to the file pointed to by the file descriptor fd, and returns the number of bytes actually written.
positioning
For random files, we can randomly specify where to read and write:
int lseek(int fd, offset_t offset, int whence);
lseek() Moves the file read-write pointer offset (negative) bytes relative to whence. On success, returns the position of the file pointer relative to the file header.
The parameter whence can take the following values:
SEEK_SET: Relative to the beginning of the file.
SEEK_CUR: Current position relative to the file read/write pointer.
SEEK_END: Relative to the end of the file.
closed
int close(int fd);
File Operation of C Library Functions--Independent of Specific Operating System Platform
create and open
FILE *fopen(const char *path, const char *mode);
fopen() to open the specified file filename, where the mode is open mode, Linux system does not distinguish between binary files and text files.
Value of mode
r, rb open in read-only mode
W, WB open in write-only mode. If the file does not exist, create it, otherwise the file is truncated
A, AB is opened by appending. If the file does not exist, create it.
r+, r+b, rb+ open in read-write mode
w+, w+b, wh+ open in read-write mode. If the file does not exist, a new file is created, otherwise the file is truncated
A+, a+b, ab+ are opened in read and append mode. If the file does not exist, create a new file
read and write
int fgetc(FILE *stream);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);
fread() reads n fields from stream, each field is size bytes, and puts the read fields into the character array referred to by ptr, returning the actual number of fields read.
write() implements writing n fields from the array referred to by buffer ptr to stream, each field is size bytes long, and returns the actual number of fields written.
closed
int fclose (FILE *stream);
Linux file system directory structure
/bin---Holds the most frequently used basic commands, such as ls, cp, mkdir, etc. The files in this directory are executable.
/boot---Some core files used when booting Linux, including some connection files and image files, such as vmlinuz, initrd.img
/dev---The directory where the device files are stored, and the application can access the actual device by reading, writing, and controlling these files.
/etc---Configuration files and subdirectories required for system administration, such as user account and password configuration files.
/home---home directory of ordinary users, each user has its own directory, usually named after the user's account.
/lib---Library file storage directory, the system's most basic dynamic connection shared library, similar to Windows DLL files.
/lost+found---Normally empty, some file fragments will be placed here when the system crashes unexpectedly or the machine shuts down unexpectedly.
/mnt---Convenient for users to temporarily mount other file systems, such as mounting the optical drive on/mnt/, enter the directory to view the contents of the optical drive
media---Automatically identifies some devices mounted in this directory, such as USB flash drives, CD-ROMs, etc.
/opt---Directory where additional installation software is stored for the host
/proc---When the operating system is running, process and kernel information (such as CPU, hard disk partition, memory information, etc.) is stored here. It is a mapping of system memory, stored in memory, and system information is obtained by directly accessing this directory.
/root----Home directory of super user
/sbin---The executable commands of super privilege users are stored in the directory, and ordinary users have no permission to execute the commands in this directory.
/tmp----Store temporary files.
/usr----Directory where system applications and files (such as commands and help files) are stored, similar to the program files directory under Windows.
/var----directories that are frequently modified are placed in this directory, such as log files
/sys---A visual representation of the kernel device tree. When a kernel object is created, the corresponding files and directories are also created in the kernel object subsystem.
/initrd--If the initrd image is used as a temporary root file system during boot, the original initial RAM file system is mapped to the/initrd directory after the/linuxrc mount to the real root file system is performed.
The above is "Linux file operation example analysis" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.