0

Further to the question I posed yesterday, in which I couldn't compile as my declaration of buffer was incorrect.

This has now been resolved as you can see in my code below, and the code will in fact compile and run! As far as I can tell, the logic in the program is correct and it should in fact work, but doesn't seem to be. This code is outputting 26 files, all numbered 0-25 (in the syntax required by the pset), but they are all broken jpegs. What have I overlooked?

I also have a couple of other questions which may in fact be related to the problem I'm having.

  1. When the program has opened the card.raw file and is reading into the buffer, once the program has completed one loop iteration, how does it know to read the next block of bytes and not just the same ones again?

  2. With fread and fwrite, should the first parameter be &buffer or just buffer?

  3. Secondly, with the fread and fwrite functions, does it matter whether I'm reading/writing one byte 512 times or 512 bytes one time, or does it only matter that I'm consistent and that the do/while loop condition is set to the correct comparison at the end?

I know this is quite a lot to ask in one question, but I'm very grateful for any insight you can give me.

Thanks in advance!

    ...code...

    do
    {
        fread(&buffer, sizeof(BYTE), BLOCKSIZE, file);

    ...code...    

        fwrite(&buffer, sizeof(BYTE), BLOCKSIZE, output);

    ...code...

    }
    while (fread(&buffer, sizeof(BYTE), BLOCKSIZE, file) == BLOCKSIZE);

    fclose(output);
    fclose(file);
    return 0;
}

1 Answer 1

1
  1. When the program has opened the card.raw file and is reading into the buffer, once the program has completed one loop iteration, how does it know to read the next block of bytes and not just the same ones again?

R.: fread() will ALWAYS read the next block. It'll only read the same block again if you force the file position indicator back with fseek() or another similar method.

  1. With fread and fwrite, should the first parameter be &buffer or just buffer?

R.: The "buffer" name works like a pointer, so you don't need to use the & operator. Just buffer will be fine.

  1. Secondly, with the fread and fwrite functions, does it matter whether I'm reading/writing one byte 512 times or 512 bytes one time, or does it only matter that I'm consistent and that the do/while loop condition is set to the correct comparison at the end?

R.: Doesn't matter for what you're trying to do, just be consistent.

Bonus question: Why is my code recovering only half the files?

R.: You're reading from the infile here: while (fread(&buffer, sizeof(BYTE), BLOCKSIZE, file) == BLOCKSIZE) and then when the loop restarts, you read from infile again here: fread(&buffer, sizeof(BYTE), BLOCKSIZE, file); Those two readings in a row without writing will make you miss half the files. Try to move that first fread() (right after the do statement) outside of your do-while loop and you'll probably see your code working.

2
  • Wow! What an awesomely comprehensive answer! I thought that the program may know where to read but I wasn't sure, and was worried that I had to go and do something with fseek again, but as it's just a simple read and output rather than a read-and-re-read like in resize. Thank you so much for seeing that on the do-while loop. Is that referring to what Zamyla was saying in the video, that it reads the data while making a comparison so it's almost as if we've accomplished two tasks at once? And yes, you're right, the code does now work! I'll edit my post to hide the code for academic honesty.
    – A.Logan
    Commented Nov 5, 2016 at 17:25
  • I don't really remember what Zamyla says, but that fread() of your do-while was indeed doing two things at once: 1 - storing a BLOCK inside the buffer; 2 - effectively checking when to stop reading the file. Sometimes people write that condition of fread(...) == 512 but forget that fread() will still read into the buffer when performing that comparison! Commented Nov 5, 2016 at 18:53

You must log in to answer this question.

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