What is the difference between fread and fwrite in C language?
The main differences between fread and fwrite in the C language lie in their functionality and usage when it comes to file operations.
- fread is used to read data from a file and store it in a specified memory location, with the following prototype:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
ptr is the memory address where the data is stored, size is the size of each element, nmemb is the number of elements to be read, and stream is the file pointer.
- The function fwrite is used to write data into a file, with the following prototype:
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
In this case, ptr is the memory address of the data to be written, size is the size of each element, nmemb is the number of elements to be written, and stream is the file pointer.
In general, fread is used to read data from a file, while fwrite is used to write data to a file. Their functions are opposite of each other, but their usage is similar.
More tutorials
Reading and Writing data using Python(Opens in a new browser tab)
How can properties files be read in Python?(Opens in a new browser tab)
Frequently Asked Questions about Writing for Donations(Opens in a new browser tab)