Read and write files in binary mode under Linux

        Recently, I need to write the memory data into a file in a project, and then read it from the file in binary mode. Since I have not been in touch with Linux development for a long time, I began to ask Du Niang. Du Niang's answer was to use fwrite to open the file in wb mode to write the data into the file, and to open the file in rb mode and use fread to read the data. The following describes the related functions and their usage instructions in detail.
        The required header file is stdio.h, and the function structure is fwrite, fread, fseek, ftell, and fstat.
1. fwrite
        size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
        Return value: return the number of data blocks actually written
        (1) buffer: is a pointer, for fwrite, it is to obtain The address of the data;
        (2) size: the single-byte number of content to be written;
        (3) count: the number of data items to be written to size bytes;
        (4) stream: the target file pointer;
        (5) Returns the number of actually written data items count.
        Description: Where to write to the file? This is related to the opening mode of the file. If it is w+, it starts writing from the address pointed to by the file pointer. After replacing the content after that, the length of the file can remain unchanged, and the position of the stream is moved by count number; if it is a+, it will be written from The end of the file is added and the file length is increased.
        Note: This function operates on files in binary form, not limited to text files
2. fread
        size_t fread(void *buffer, size_t size, size_t count, FILE *stream) ;
        Return value: Returns the actual number of items written, if Greater than count means that an error occurred. Also, the file location indicator cannot be determined after an error is generated. If other streams or buffers are null pointers, or the number of bytes written in unicode mode is odd, this function sets errno to EINVAL and returns 0.
        (1) buffer: The memory address used to receive data;
        (2) size : The number of bytes of each data item to be read, in bytes;
        (3) count: count data items to be read, each data item size bytes.;
        (4) stream: target file pointer;
3 . fseek
        int fseek(FILE * stream, long offset, int whence);
        Return value: return 0 for success, -1 for failure, and errno will store the error code.
        (1) stream: target file pointer;
        (2) offset: offset relative to whence;
        (3) whence: absolute position.
        Description: fseek() is used to move the read and write position of the file stream. The parameter stream is the opened file pointer, and the parameter offset is the displacement number to move the read and write position according to the parameter whence.

        Note 1: The parameter whence is one of the following:

        ① SEEK_SET is the new read and write position from the offset displacement from the beginning of the file.
        ② SEEK_CUR increases the offset amount by the current read and write position.
        ③ SEEK_END points the read and write position to the end of the file and then increases the offset displacement.
        Note 2: When the whence value is SEEK_CUR or SEEK_END, the offset parameter allows negative values.
        Note 3: The following are special usages:
        ① When moving the read/write position to the beginning of the file: fseek(FILE *stream, 0, SEEK_SET);
        ② When moving the read/write position to the end of the file: fseek(FILE *stream, 0,SEEK_END).

5. ftell

        long ftell(FILE * stream);

        Return value: Returns the current read/write position on success, -1 on failure.
        (1) stream: pointer to an opened file.
6. fstat
        int fstat(int filedes, struct stat *buf);
        Return value: return 0 for success, -1 for failure, and the error code is stored in errno.
        (1) fildes: the opened file pointer to obtain the status;
        (2) buf: the storage address of the obtained file status;

        Description: fstat() is used to copy the file status pointed to by the parameter fildes to the structure pointed to by the parameter buf (struct stat).


I haven't written sample code for the time being, I will post it when I have time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324361768&siteId=291194637