62.0k views
0 votes
What do the functions fread() and fwrite() do?

User Somnium
by
7.9k points

1 Answer

3 votes

Final answer:

In C programming, fread() is used to read bytes from a file into memory, while fwrite() is used to write bytes from memory to a file. Both functions work with a file pointer from fopen() and require specifying the size and number of elements to process.

Step-by-step explanation:

The functions fread() and fwrite() are used in C programming for reading from and writing to files, respectively. The fread() function reads a specified number of bytes from a given file into memory, whereas fwrite() writes a specified number of bytes into a file from memory. Both functions require a pointer to a file, which is provided by the fopen() function, as well as a size and a count that represent the size of each element and the number of elements to be read or written.

For example, to read 10 integers from a binary file, you can use fread(buffer, sizeof(int), 10, filePtr), where buffer is an array of integers and filePtr is the file pointer. Similarly, to write 10 integers to a file, you can use fwrite(buffer, sizeof(int), 10, filePtr).

User Kewei Shang
by
8.9k points