Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the correct way for a Linux system to open a file

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

What is the correct way for Linux system to open files? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

The open function for opening a file involves a header file: directory where the ubuntu header file is located: / usr/include/x86_64-linux-gnu/sys # include#include#include specific function: l

Parameter explanation:

Function: give the file a new file descriptor

Pathname: specify a file path

Flags: read file mode O_RDONLY, O_WRONLY, or O_RDWR

Mode: permission assignment for reading files

S_IRWXU = = 00700 users have read and write execution permissions S_IRUSR 00400 user has read permission S_IWUSR 00200 user has write permission S_IXUSR 00100 user has execute permission S_IRWXG 00070

Group has read, write and execute permission S_IRGRP 00040

Group has read permission S_IWGRP 00020

Group has write permission S_IXGRP 00010

Group has execute permission S_IRWXO 00007 others have read, write and execute permission S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission

Return value: successfully returns a non-negative integer of the file descriptor

# include # include int main (int argc, char*argv []) {int fd= 0; fd=open (". / open_1.c", O_RDONLY); printf ("fd=% d\ r\ n", fd); return 0;} 1234567891011121314 output result: fd= 3 12 only reads the file here, try to read the contents of this file and output it to the screen Read function will be used here: man 2 read view specific explanation # includessize_t read (int fd, void * buf, size_t count); function: read count bytes from file descriptor fd and put them in buf; return value: count = 0 return 0 count is not zero, return the current location of the file if the file is not empty; if failed, return-1

Read result: buffer= # include # include int display_file (int,int); int main (int argc, char*argv []) {int fd= 0; fd=open (". / open_1.c", O_RDONLY); / / printf ("fd=% d\ r\ n", fd); display_file (fd, 1024); return 0;} int display_file (int fd, int count) {char buffer [100] Memset (buffer, 0, sizeof (buffer)); if (0 > fd | | 0 > = count) return-1; int read_num = read (fd,buffer,count); if (read_num return-1; else return read_num; fprintf (stdout, "buffer=% s read_num =% d", buffer, read_num) } read_num = 520 coincidentally read 520 bytes 1234567891011121314151718192021223242527282930313233 it should be noted that the space size requested by buffer must be greater than or equal to count, otherwise a bus error will be reported.

Write file man 2 write # includessize_t write (int fd, const void * buf, size_t count); parameter: fd: file descriptor to be written; buf: write the contents of buf to fd count: number of bytes written at a time

# include # include int display_file (int,char *, int); int main (int argc, char*argv []) {int fd= 0; int fdw = 0; char buffer [1024]; memset (buffer, 0, sizeof (buffer)); fd=open (". / open", O_RDONLY); fdw = open (". / open_2", O_RDWR | O_CREAT, S_IWUSR | S_IRUSR | S_IROTH) / / printf ("fd =% d\ r\ n", fd); display_file (fd, buffer,1024); write_to_file (fdw,buffer,1024); return 0;} int display_file (int fd, char * buf, int count) {char * buffer = NULL; buffer = buf; if (0 > fd | | 0 > = count | | NULL = buf) return-1 Int read_num = read (fd,buffer,count); if (read_num return-1; else return read_num; fprintf (stdout, "buffer=% s read_num =% d\ r\ n", buffer, read_num);} int write_to_file (int fd,char* buff, int count) {int write_num = write (fd,buff, count) If (write_num return-1; else return write_num;}) this is the answer to the question about what is the right way to open a file in the Linux system. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report