Linux file operations – standard c library for file operations

The difference between Linux and standard c library in file operations

1. Source

From the perspective of source, the two can be distinguished very well, and this is also the most obvious difference between the two:

  • open is a UNIX system call function (including LINUX, etc.). It returns a file descriptor (File Descriptor), which is the name of the file in the file descriptor table. Index. .·
  • fopen is a C language library function in the ANSIC standard. Different kernel apis should be called in different systems. What is returned is a pointer to the file structure.

2. Transplantability

  • This can be inferred from the above sources. ‘fopen’ is a C standard function, so it has good portability, while ‘open’ is a UNIX system call, which means it has good portability. Sex is limited. For example, you can only run ‘fopen’ under windows, but cannot run ‘open’. If you want to run ‘open’ under windows, you can only use the API function ‘CreateFile’ for similar functions under windows.

3. Scope of application

  • open returns a file descriptor, and file descriptor is an important concept under UNIX system. All devices under UNIX operate in the form of files. Such as network sockets, hardware devices, etc. Of course, this includes operating regular files. (Only open can be used in some specific cases)
  • fopen is used to manipulateordinary regular files (Regular File).
  • Both can manipulate ordinary formal files, but fopen is usually recommended

4. File IO level

  • If you look at it from the perspective of file I0, the former belongs to the low-level I0 function, and the latter belongs to the high-level I0 function. The simple distinction criterion between low-level and high-level is: Who is closer to the system kernel. Low-level file IO runs in the kernel mode, and high-level file IO runs in the user mode.

5. Buffering

  • 1. Buffer file system The characteristics of the buffer file system are: Open a “buffer” in the memory, which is used for each file in the program. When performing a file reading operation, the file from the disk will be The data is first read into the memory “buffer”, and then the required data is read out from the memory “buffer” when it is full. When performing a file writing operation, the data is first written into the memory “buffer”, and then the file is written after the memory “buffer” is full. It can be seen from this that the size of thememory “buffer” affects the actual number of external memory operations. The larger the memory “buffer”, the fewer the number of external memory operations and the faster and more efficient the execution. . Generally speaking, the size of the file “buffer” depends on the machine. fopen, fclose, fread, fwrite, fgetc, fgets, fputc, fputs, freopen, fseek, ftell, rewind, etc.
  • 2. Non-buffered file system The buffered file system uses file structure pointers to manage files. Files are accessed through file pointers, which can read and write characters, strings, Format data and can also read and write binary data. The non-buffered file system relies on the operating system. It reads and writes files through the functions of the operating system. It is a system-level input and output. It does not have a file structure pointer and can only read and write binary files, but it has high efficiency and speed. Fast, since the ANSI standard no longer includes non-buffered file systems, it is recommended that you should not choose it. open, close, read, write, getc, getchar, putc, putchar, etc.
  • To sum it up in one sentence,open has no buffering and fopen has buffering. The former is used in conjunction with read, write, etc., and the latter is used in conjunction with fread, fwrite, etc.
  • Using the fopen function, since there is a buffer in the user mode, it reduces the switching between the user mode and the kernel mode when performing file reading and writing operations (switching to the kernel mode still requires calling the system call AP:read, wite); while using the open function, you need to switch between the kernel mode and the user mode every time when reading and writing files. This shows that if you access files sequentially, the fopen series of functions are faster than directly calling the open series of functions; if The opposite is true for random access files.

Standard c library for file operations

1.fopen function

You can use the fopen function to create or open an existing file in the Linux environment. The return value is the file name of the FILE type. The function prototype is as follows:

FILE *fopen(const char *filename, const char *mode);
const char *filename Filename
,const char *mode Permissions to open files (method)

There are many ways to open files. The following is a summary of commonly used file opening methods.

< /table>

2.fclose function

Just like Linux’s file operations, after opening a file and performing a series of operations on the file, if you need to close the file, flose is called. The function prototype is as follows:

 int fclose( FILE *fp );

The parameter is the return value of opening the file, similar to the file descriptor

The fclose( ) function returns zero if the file is closed successfully, or EOF if an error occurs while closing the file. This function actually clears the data in the buffer, closes the file, and releases all memory used for the file. EOF is a constant defined in the header file stdio.h.

3.fwrite function

The function prototype is as follows:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Method Explanation
r Open an existing text file, allowing the file to be read, the file must exist
r + Open an already Some text files allow reading and writing files. The file must exist
w Open the file for writing. If the file does not exist, create the file; if the file exists, overwrite the original data and rewrite the data; when writing out, overwrite the original data of the file, that is, clear the file length to 0, delete the file content, and rewrite the file content< /strong>
w + Open an existing text file for reading and writing. If the file does not exist, create the file; if the file exists, overwrite the original data and rewrite the data; when writing out, overwrite the original data of the file, that is, clear the file length to 0, delete the file content, and rewrite the file content< /strong>
a Open the file for appending. If the file does not exist, the file will be created; if the file exists, the newly written data will be appended to the end of the file, and the original data of the file will be retained
a + Open the file for reading and writing in append mode. If the file does not exist, the file will be created; if the file exists, the newly written data will be appended to the end of the file, and the original data of the file will be retained
void *ptr Buffer (where content is stored)
size_t size Write Size in bytes
size_t nmemb Number of bytes written
FILE *stream Content input location (transfer content to the specified location)

4.fread function

The function prototype is as follows:

size_t fread(void *ptr, size_t size, sizet nmemb, FILE *stream);
void *ptr Buffer (where the content is stored after reading the content)
size_t size Read size in bytes td>
sizet nmemb Read Number of bytes
FILE *stream The location where the content is initially stored (will be read soon)

Code display

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *fp;
char *buf = "Hello word";
char readBuf[128] = {0};

fp = fopen("Luo.text","w + ");//Open Luo.text, create one if it does not exist

fwrite(buf,sizeof(char),strlen(buf),fp);//buf is a string, so only the second parameter is the character byte size, and the third parameter is the string length. The content is written to fp
// fwrite(buf,sizeof(char)*strlen(buf),1),fp);//You can also calculate the total length only once
fseek(fp,0,SEEK_SET);//Move the cursor to facilitate reading
\t
fread(readBuf,sizeof(char),strlen(buf),fp);//Read the contents of fp into readBuf
// fread(readBuf,sizeof(char)*strlen(buf),1),fp);//Similar to the above

printf("read context is %s\\
",readBuf);

    return 0;
}
                     

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. CS entry skill treeLinux introductionFirst introduction to Linux 38134 people are learning the system