-1

I need a plain English explanation of how the while loop operates for the fread statement.

After reading in the first 512 bytes, how does it loop to the next 512 bytes till it gets to the last block?

1
  • 1
    If you'll edit your post to show the while loop, maybe we can answer. ;-)
    – Cliff B
    Jun 11, 2020 at 6:05

2 Answers 2

-1

Would be nice to see some actual code so we have something to work with but I am assuming your question is how this while(fread(bytes, 1, 512, file) == 512) iterates through the file. As I understand it, it just reads the next block of bytes everytime it repeats, nothing fancy. In this case it would repeat until it does not read an entire block of 512 bytes. https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fread?view=vs-2019 for further information.

1

Fread() function allows you to iterate over file if combining with the while loop. Basically the fread() function will look at the file and search for a block size of 1 byte 512 times or similarly you could do 1 time a block of 512 bytes.

After having completing the first run, the file pointer associated with stream (file in your case) is increased by the number of bytes actually read. Meaning that the pointer is moving to the end of the first block you just read from file. Reference : here

If you combine it with a while loop, it will redo the same process until it no longer meets your condition (== 512).

In a nutshell, the incremental process (that you would expect in a while loop to make it efficient) is part of the fread function.

You must log in to answer this question.

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