[Linux] System file interface

Table of contents

1. C file interface

1、fopen

2、fprintf

3、snprintf

2. System file IO

1、open

2、write

3、read

4. The relationship between C file interface and system file IO


1. C file interface

1、fopen

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

The return value type of the   fopen function is FILE . In the parameter list, path is the file path, and mode  is the opening method.

 There are several ways to open a file:

r Open a text file for reading.
The stream is at the beginning of the file.
r+ Open for reading and writing.
The stream is at the beginning of the file.
w Open the file for writing. If the file does not exist, it is created, otherwise the file is emptied.
The stream is at the beginning of the file.
w+ Open for reading and writing. If the file does not exist, it is created, otherwise the file is emptied.
The stream is at the beginning of the file.
a Open for appending (writing at the end of the file).
If the file does not exist, it is created.
The stream is at the end of the file.
a+ Open for reading and appending (writing at the end of the file).
If the file does not exist, it is created. The initial file position for reading is at the beginning of the file,
but output is always appended to the end of the file.

2、fprintf

int fprintf(FILE *stream, const char *format, ...);

Unlike printf , printf prints messages to the display by default, while fprintf can specify a file stream and print to a specified file.

Write the following code:

  1 #include <stdio.h>
  2 
  3 #define LOG "log.txt"
  4 
  5 int main()
  6 {
  7   FILE* fp = fopen(LOG, "w");
  8   if(fp == NULL)
  9   {
 10     perror("fopen");
 11     return 1;
 12   }
 13 
 14   const char* msg = "hello world";                                                                                                      
 15   int cnt = 5;
 16   while(cnt)
 17   {
 18     fprintf(fp, "%s: %d: ljb\n", msg, cnt);
 19     //fputs(msg, fp);
 20     cnt--;
 21   }
 22 
 23   fclose(fp);
 24   return 0;
 25 }

Run observations:

 You can also use the fprintf function to print to the display file to achieve the same effect as the printf function:

3、snprintf

int snprintf(char *str, size_t size, const char *format, ...);

 The snprintf function can print the content into the buffer , andcontrol the length of the print by setting the parameter size .

Write the following code:

  1 #include <stdio.h>  
  2   
  3 #define LOG "log.txt"  
  4   
  5 int main()  
  6 {  
  7   FILE* fp = fopen(LOG, "w");  
  8   if(fp == NULL)  
  9   {  
 10     perror("fopen");
 11     return 1;
 12   }
 13   
 14   const char* msg = "hello world";
 15   int cnt = 5;
 16   while(cnt)
 17   {
 18     char buffer[256];
 19     snprintf(buffer, sizeof(buffer), "%s: %d:ljb\n", msg, cnt);
 20     printf("%s", buffer);
 21   }                                                                                                                                     
 22                                                                                                                               
 23   fclose(fp);                                                                                                                 
 24   return 0;                                                                                                                   
 25 } 

 Run observations:

2. System file IO

1、open

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

In the parameter list of the  open function, pathname is the file path and file name, flags is the option to open the file (passed in bitmap form), and mode is the permission to open the file. The return value is a file descriptor .

flags parameter:

  • O_RDONLY: open for reading only
  • O_WRONLY: Open for writing only
  • O_RDWR: read, write open
  • O_CREAT: If the file does not exist, create it. You need to use the mode option to indicate the access permissions of the new file
  • O_APPEND: Additional writing (need to be used with O_WRONLY)
  • O_TRUNC: empty the file

return value:

  • success: newly opened file descriptor
  • Failed: -1
     

 Write the following code:

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/stat.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 #include <errno.h>
  7 #include <string.h>
  8 
  9 #define LOG "log.txt"
 10 
 11 int main()
 12 {
 13   umask(0);                                                                                                                             
 14   int fd = open(LOG, O_CREAT | O_WRONLY, 0666);                                                                 
 15   if(fd == -1)                                                                                                  
 16   {                                                                                                             
 17     printf("fd: %d, error: %d, errstring: %s\n", fd, errno, strerror(errno));                                   
 18   }                                                                                                             
 19                                                                                                                 
 20   else                                                                                                          
 21     printf("fd: %d, error: %d, errstring: %s\n", fd, errno, strerror(errno));                                   
 22                                                                                                                 
 23   close(fd);                                                                                                    
 24                                                                                                                 
 25   return 0;                                                                                                     
 26 }   

 Among them, O_CREAT and O_WRONLY are macro definitions, and the parameters flags are passed .  O_CREAT is to automatically create the file if it does not exist.  O_WRONLY opens the file in read-only mode. It should be noted that they will not clear the content of the original file. When writing the next time, although it is still written from the beginning, the part of the original content that has not been overwritten will still remain .

Run observations:

As shown, the file descriptor  returned by opening the file is 3 .

The permission of the log.txt file  that was just created is  666  .

2、write

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

In the parameter list of the  write function, buf is the first address of the buffer,  and count  is the number of bytes of data expected to be written in this read
. Return value: How many bytes of data are actually written.

Write the following code:

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/stat.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 #include <errno.h>
  7 #include <string.h>
  8 
  9 #define LOG "log.txt"
 10 
 11 int main()
 12 {                                                                                                                                       
 13   umask(0);
 14   int fd = open(LOG, O_CREAT | O_WRONLY, 0666);
 15   if(fd == -1)
 16   {
 17     printf("fd: %d, error: %d, errstring: %s\n", fd, errno, strerror(errno));
 18   }
 19   else
 20     printf("fd: %d, error: %d, errstring: %s\n", fd, errno, strerror(errno));
 21 
 22   const char* msg = "hello world";
 23   int cnt = 5;
 24   while(cnt)
 25   {
 26     char line[128];
 27     snprintf(line, sizeof(line), "%s, %d\n", msg, cnt);
 28 
 29     write(fd, line, strlen(line));
 30     cnt--;
 31   }
 32 
 33   close(fd);
 34 
 35   return 0;
 36 }

 It should be noted that the number of data bytes here uses strlen(line)  instead of strlen(line) + 1  . This is because the character string must be added with '\0' at the end , which is the regulation of the C language, not the regulation of the file.

3、read

ssize_t read(int fd, void *buf, size_t count);

In the parameter list of the  read  function, buf is the first address of the buffer,  and count  is the number of bytes of data expected to be read this time
. Return value: how many bytes of data are actually read. 

 Write the following code:

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/stat.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 #include <errno.h>
  7 #include <string.h>
  8 
  9 #define LOG "log.txt"
 10 
 11 int main()
 12 {                                                                                                                                    
 13   umask(0);                                                                                                                          
 14   int fd = open(LOG, O_RDONLY);                                                                                                      
 15   if(fd == -1)                                                                                                                       
 16   {
 17     printf("fd: %d, error: %d, errstring: %s\n", fd, errno, strerror(errno));
 18   }
 19   else
 20     printf("fd: %d, error: %d, errstring: %s\n", fd, errno, strerror(errno));
 21 
 22   char buffer[1024];
 23 
 24   ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
 25   if(n > 0)
 26   {
 27     buffer[n] = '\0';                                                                                                                   
 28     printf("%s\n", buffer);
 29   }
 30 
 31   close(fd);
 32 
 33   return 0;
 34 }

 When using the system interface for IO, be sure to pay attention to the problem of '\0' .

Run observations:

4. The relationship between C file interface and system file IO

 fopen, fclose, fwrite, fputs, fread, and fgets are all functions in the C standard library, which we call library functions (libc).
 open, close, write, and read all belong to the interface provided by the system, called the system call interface.

 It can be considered that the functions of the f# series are all encapsulation of system calls, which is convenient for secondary development.


That’s all for the content of the system file interface. I hope you will support me a lot. If there is something wrong, please correct me, thank you!

Guess you like

Origin blog.csdn.net/weixin_74078718/article/details/130202727