Here is an excerpt of the code I'm trying now in relation to my previous problems in cs50 'Recover'.
I've tried everything with fread
, but for some reason it simply isn't working the way I want it to.
In this loop, I'm trying to figure out how fread
actually works.
My question with fread
is - does it read 512 bytes of data 1 at a time from file pointed to (rawdata
) each time it's called? In that case, my code should work since the loop is running indefinitely, calling the function over and over and hence moving the stream position/file cursor (I don't know what its called) 512 bytes at a time. I have a break from the loop with feof(rawdata)
.
I'm using this small program to aid me with Recover from cs50 pset4.
// In a loop, until the end of file has been reached,
while (true) {
// Zeroing counter
jpg_counter = 0;
// Reading 512 bytes into the array of bytes
fread(bytes, 512, 1, rawdata);
// Searching the 512 bytes for JPEG signatures - bytes[3] with bitwise AND
if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3] & 0xf0) == 0xe0) {
jpg_counter++;
}
// If found JPG, add total and keep going till end of file
if (jpg_counter != 0) {
total++;
}
//feof returns a non zero value only at the end of a file
if (feof(rawdata) != 0) {
break;
}
}
printf("%i\n", total);