c program problem

cyber

my name defines me
algorithm..

opens a file containing hex values (jpeg image)

obtains the first four bytes and compares with 0xe0ffd8ff or 0xe1ffd8ff to check whether it is the start of a jpeg image.

if so, copies 512 bytes into the first file..(jpeg's are stored in 512 byte block)

repeats the process...


problem...

goes into an infinite loop at the second while condition




#include<stdio.h>
#include<stdint.h>


typedef uint32_t DWORD;


typedef struct
{
DWORD initial;


} CHECK;




int main(void)
{
FILE* fp1;


char title[8];




CHECK jp;
int count = 1;


if ((fp1 = fopen("card.raw", "r")) == NULL)
{
printf("file1 open error");
return 1;
}
fread(&jp, sizeof(CHECK), 1, fp1);


while ((jp.initial != 0xe0ffd8ff && jp.initial != 0xe1ffd8ff))
{
fseek(fp1, 508, SEEK_CUR);
fread(&jp, sizeof(CHECK), 1, fp1);


}
fseek(fp1, -4, SEEK_CUR);


do
{
FILE* fp2;
sprintf(title, "%3d.jpg", count);


if ((fp2 = fopen(title, "w")) == NULL)
{
printf("file open error");
}




fgets(buf, sizeof(buf), fp2);
fread(&jp, sizeof(CHECK), 1, fp1);




while ((jp.initial != 0xe0ffd8ff && jp.initial != 0xe1ffd8ff))
{
fseek(fp1, -4, SEEK_CUR);
fwrite(fp1, 512, 1, fp2);


}
fclose(fp2);
count++;
}
while (!feof(fp1));
return 0;
}

fgets(buf, sizeof(buf), fp2);
fread(&jp, sizeof(CHECK), 1, fp1);




while ((jp.initial != 0xe0ffd8ff && jp.initial != 0xe1ffd8ff))
{

ignore fgets it is commented
 

nightcrawler

Broken In
while ((jp.initial != 0xe0ffd8ff && jp.initial != 0xe1ffd8ff))
{
Didn't have time to check the code in detail. But correct me if I am wrong here. Don't you think your code above is a bit faulty? As from the looks of it it will run through the loop cause the condition cannot be met. (what you need is either clause). Also you need to have control in place if the file you get is not according to the format that you are looking for.
 
Top Bottom