1

I'm beginning to wrap my head around the week 4 problem set, and I'm confused about how the fread and fwrite functions work.

As I understand it, fread tells the computer to copy data into RAM, and fwrite tells it to write data to a file, which is pointed to with something like "&buffer", such as in the distribution code for week 4's lab, Volume:

Considering an example from the distribution code for filter:

// Copy header from input file to output file
uint8_t header[HEADER_SIZE];
fread(header, HEADER_SIZE, 1, input);
fwrite(header, HEADER_SIZE, 1, output);

// Read samples from input file and write updated data to output file
uint16_t buffer;

while (fread(&buffer, sizeof(uint16_t), 1, input))
{
    buffer = buffer * factor;
    fwrite(&buffer, sizeof(uint16_t), 1, output);

Assuming I understand basically what's happening when the fread and fwrite functions are called - How is it that in the above example, the second time fwrite is called it doesn't overwrite the data already stored at the beginning of the file "output", aka the header? How does it know to start with the 45th byte? (the header size is 44 bytes).

2 Answers 2

2

From this article:

One of the attributes of an open file is its file position that keeps track of where in the file the next character is to be read or written. On GNU systems, and all POSIX.1 systems, the file position is simply an integer representing the number of bytes from the beginning of the file.

The file position is normally set to the beginning of the file when it is opened, and each time a character is read or written, the file position is incremented. In other words, access to the file is normally sequential.

2
  • do you reckon that every time calling fwrite() when fopen( , 'w') it writes after the last bit until fclose() is called?
    – F.Yang
    Nov 30, 2021 at 4:38
  • I'm also doing the lab 4 and have the same question as yours.
    – F.Yang
    Nov 30, 2021 at 4:39
1

The answer lies in how the file was opened. When opening a file with fopen (which must have been called earlier in the file), you can pass a parameter as the mode to open the file in. Some of these parameters include: "r", to read; "w", to write; and "a", to append. If you opened a file in read mode, you could not write to the file at all. In write mode, fwrite will also start writing to the file at the beginning of the file. But, in append mode, fwrite will start writing at the end of the file.

If you want to get into even more detail about how fwrite knows where the end is, you could look up its documentation or source code, but you could also take a look at the article that DinoCoderSaurus linked.

I hope that helps clear things up. If it did, feel free to hit the check mark to accept the answer, but, if not, leave me a comment and I can try to explain further/better. :)

3
  • Everything in your answer is true, but doesn't answer the question. It doesn't explain how the file pointer keeps track of where to write the next block of data to be written to a file. "Right answer to the wrong question." Good try though. ;-)
    – Cliff B
    Jan 15, 2021 at 20:52
  • Yeah, I guess I didn't get the real question and put too much weight into the example code. Jan 15, 2021 at 21:11
  • Thanks so much for the answer Robert - looks like what I needed to understand was this concept of "file position". I also didn't know that you can append to the end of the file with "a". Now I'm reading up on how to open a file at a specific position, and the seek function. Thanks all! Jan 17, 2021 at 18:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .