Hi again,
I am working on a HOMEWORK assignment to recover jpg images from a .raw file. As this is something that I will be graded on, I am NOT asking you do "do it all for me" I am asking for hints and help so that I may continue to learn (and get a good grade at the same timehttp://im.cprogramming.com/images/smilies/wink.png).
The assignment I am currently working on involves recovering a jpg image by looking for the start of a jpeg, and then copying 512 byte chunks to an image file until the start of the next image is seen. I believe I am close to having a fully working program, The part I am stuck on is when I run the program I get a Segmentation fault.
If you could look over my program (below) and give me your opinion on what is wrong; that would be GREAT!!!
Code:
#include <stdio.h>
#include <stdlib.h>
#define false 0
#define true 1
typedef int bool;
typedef unsigned char BYTE;
int main(void) {
FILE*outptr;
// open input file
FILE* inptr = fopen("card.raw", "r");
if (inptr == NULL)
{
printf("Sorry, me noes able to open the file!!!\n");
return 2;
}
bool jpegfoundbefore = false;
int number = 0;
int block_size = 512;
BYTE buffer [512];
char image_name[8];
// untill eof is reached
while (! feof(inptr) == 0)
{
//copy 512 bytes into buffer
fread(buffer, block_size, 1, inptr);
{
// if a previous jpeg was found close file
if (jpegfoundbefore == true)
{
fclose(outptr);
}
jpegfoundbefore = true;
// name the image
sprintf(image_name, "%.3d.jpg", number);
number++;
// create the new jpg image file
outptr = fopen (image_name, "w");
if (outptr == NULL)
{
printf("could not create jpeg file\n");
return 2;
}
// write 512 byte block to current jpeg file
fwrite(buffer, block_size, 1, outptr);
}
// continue writing parts of a jpg to file on disk
if (jpegfoundbefore == true)
{
fwrite(buffer, block_size, 1, outptr);
}
}
// close card.raw
fclose(inptr);
// close last jpg
fclose(outptr);
}