-1

Below is the code that i have written when i execute the code i get jpg images but they are corrupted.Any help regarding it?

     //reading the sector and checking the condition
 do { fread(from buffer)
     if(condition)
     open the file in write mode;
     fwrite();
     fread(from buffer of size 512);  //next sector
 do 
 {   
     fwrite(into img from buffer);
     fread(from buffer of size 512); //next sector

  }  while(!(initialcondition));
 close(img);
} while(end of file)
0

The whole file is composed of 512 byte blocks so you should always read exactly 512 bytes. It goes something like this:

  1. Read 512 bytes from file.
  2. Check whether bytes starting have a JPEG signature
    • YES: close current file and create a new one
    • NO: write to the current file if it is not null (e.g. at the very start of the loop)
  3. Go to the first step and repeat until the whole file is read.

I hope this will help you :)

PS: also I would suggest you to save the number which is returned by fread (number of bytes it's actually read) and pass it to fwrite as the number of bytes to be written. If not, when reading last chunk of the last file, you can get less that 512 bytes and other bytes will remain intact, thus you will write a part of the previous chunk after the actual end of the last foto. I'm not sure that this issue may cause trouble, because I took this approach in the first time and everything worked like a charm. It's good practice nonetheless :)

You must log in to answer this question.

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