0

I've been itching at this code for hours now, it doesn't output any images, everytime i try to debug it it comes to the first while statement and immediatly returns 0, i have no idea why

int main(int argc, char* argv[])
{

int firststar=0;
int jpgcount=0;
unsigned char buffer[512];
FILE* inptr = fopen("card.raw", "r");

while(fread(buffer, 512,1,inptr) == 1)
{

    if(buffer[0]== 0xff && buffer[1]== 0xd8 && buffer[2]== 0xff && (buffer[3]==0xe0 || buffer[3]==0xe1))
    {  
        char title[9];

 sprintf(title,"%d.jpg",jpgcount);

        FILE* outptr = fopen("title","a");

        fwrite(buffer,512,1,outptr);

        firststar=1;

         jpgcount++;

do
         {  
         fread(buffer, 512,1,inptr);
         fwrite(buffer,512,1,outptr);
         } while(buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff);

    fclose(outptr);

    } 
}
fclose(inptr);

}

1 Answer 1

1

Look at your file open:

        FILE* outptr = fopen("title","a");

You are literally naming every file "title". If you do an ls -al you will probably see it in the listing. Maybe you should remove the quotes around title so that it uses the contents of the variable instead?

In addition, it is being opened for append, not for write. Append mode will just add to the end of the file if it exists. Write mode will delete any existing file with that name and start fresh. The net is that you're creating a file called 'title' of about 2 MB size.

There are additional problems, but that wasn't your question. I wouldn't want to deprive you of the chance to debug them on your own. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • I edited that and the code worked! thanks! my code right now outputs images fine but does not pass check50, it gives me a random error, i updated the code :)
    – Omar Wagih
    Commented Mar 17, 2016 at 12:10
  • @OmarWagih if you have follow-up questions, you should update your question, not change it entirely. if you have a new question, you may start a new post!
    – kzidane
    Commented Mar 24, 2016 at 9:20

You must log in to answer this question.

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