What are the differences between Linux file operations open, close, write, read, creat, lseek, and fopen, fread, fwrite, fseek, fclose, fgetc, fputc, feof and before?

Preface

1. The difference between open and fopen, close and fclose

2. The difference between read and fread, write and fwrite

3. Use of lseek and fseek

4. Use of creat, fgetc, fputc, feof

Preface

This is the learning experience recorded when learning Linux file system programming.

From a source perspective, functions such as open are different from functions such as fopen:

  • openIt is a LUNIX system call function that returns a file descriptor (File Descriptor), which is the index of the file in the file descriptor table.
  • fopenIt is a C language library function in the ANSIC standard and should call different kernel apis in different systems. What is returned is a pointer to the file structure.

From the perspective of porting: fopen is a C standard function and has good portability; while open is a LUNIX system call and has limited portability. For similar functions under windows, use the API function `CreateFile`.

From the scope of application: open returns the file descriptor. All devices under LUNIX operate in the form of files. Such as network sockets, hardware devices, etc. Of course, including operating regular files (Regular File). fopen is used to manipulate regular files (Regular File)

From a buffering perspective: openno buffering, fopenwith buffering. The buffered file system uses file structure pointers to manage files, and files are accessed through file pointers. Buffering means operating on the buffer first and then on the file. For example, when performing a file writing operation, the data is first written into the memory "buffer", and then the data is written into the file after the memory "buffer" is full. The non-buffered file system relies on the operating system. It reads and writes files through the functions of the operating system, which is system-level input and output.

1. The difference between open and fopen, close and fclose

open and close are used together, fopen and fclose are used together

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

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

The open function returns the file descriptor, which is the index of the file. fopen returns the file pointer, which is accessed through the file pointer.

 int close(int fd);
 int fclose(FILE *fp);

The two methods of use are similar.


2. The difference between read and fread, write and fwrite

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

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

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

 size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

ptr is the buffer, fwrite writes data from the buffer to the file, and fread reads the data from the file to the buffer.

The return value of read and write is the byte size read and written, and the return value of fread and fwrite depends on the third parameter: the number of reads and writes.

3. Use of lseek and fseek

off_t lseek(int fd, off_t offset, int whence);

int fseek(FILE *stream, long offset, int whence);

Both are used the same, int whence has three positions:
        SEEK_SET cursor points to the head, SEEK_CUR cursor points to the current position, SEEK_END cursor points to the tail

        offset is the offset relative to the position. Negative numbers of the offset move forward, and positive numbers move backward.
        lseek(fd,0,SEEK_END) uses its return value to calculate the file size.

4. Use of creat, fgetc, fputc, feof

 int creat(const char*pathname,mode_t mode)

                            Absolute path Mode: Macro representation Number
                                                               S_IRUSR 4 Readable
                                                               S_IWUSR 2 Writable  
                                                               S_IXUSR 1 Executable    
                                                               S_IRWXU 7 Readable, writable, and executable 

int fputc(int c, FILE *stream);

Write a character to the fp file stream. To write multiple characters, you can use a loop to write one by one.

int fgetc(FILE *stream);

To get a character from the file, you can loop to get the characters one by one, with feof

 int feof(FILE *stream);

Determine whether the end of the file stream is reached. The return value is 0 if the end is not reached. The return value is non-0 when the end is reached.

 do
   {
      c = fgetc(fp);
      if( feof(fp) )
      {
          break ;
      }
      printf("%c", c);
   }while(1);

Reference article: https://www.cnblogs.com/NickyYe/p/5497659.html

   


Guess you like

Origin blog.csdn.net/qq_44848795/article/details/121907858