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?
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.
Sign up to join this communityI 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?
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.
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.