I run my code which seems right to me but nothing happens! No images appear in my jpeg file!! Please help me I am stuck. Thank you

[code hidden to comply to academic honesty]

// Checks if file is indeed a JPEG by checking if the first 4
// sequence of bites match the sequence of bytes that a JPEG must
// have as mentioned in walkthrough
if(buffer[0]==0xff && buffer[1]== 0xd8 && buffer[3] == 0xff && (buffer[3]==0xe0 || buffer[3]==0xe1))

[code hidden to comply to academic honesty]

1 Answer 1


The basic reason your program doesn't produce any jpgs, its because it doesn't find any. That's because you have a typo in the following line:

if(buffer[0]==0xff && buffer[1]== 0xd8 && buffer[3] == 0xff && (buffer[3]==0xe0 || buffer[3]==0xe1))

You are checking buffer[0], then buffer[1] and then... buffer[3], while it should be buffer[2].

Although even if you fix this mistake, and your program finds the jpgs (which it does) you still have a flaw in your logic.

Consider what happens when you find the start of a jpg.

  1. You create a new filename (you are doing that wrong too btw),
  2. you create a new file with that name,
  3. write the first 512 bytes of this jpg,
  4. and increment the found jpgs by one.

Then what? You don't write any additional bytes to the image, you don't close each image, you just write the first 512 bytes of each image. I would suggest you re-think your logic.

  • Thnx Chris G I have lots of thinking to do it seems hahahahhaa but nevertheless I appreciate your help.. btw why was my code formatted .. I do not understand? Aug 20, 2015 at 10:01
  • Because if you don't specify the language your code is in, the forum doesn't show your code correctly. Take a look at that: meta.stackexchange.com/questions/184108/…. This will also help you and us in future questions. Btw if this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.
    – ChrisG
    Aug 20, 2015 at 10:13
  • Ok good to know! I will work on my pset using your guidelines and as soon as I am done I will ofcourse give you the green check mark! Honestly thank you though for answering my questions! Aug 20, 2015 at 10:35
  • We're always here to help! :) Keep coding!
    – ChrisG
    Aug 20, 2015 at 10:36
  • Thnx!! so why I am opening the file in a wrong way though? I am using the same thing as copy.c Aug 20, 2015 at 10:45

You must log in to answer this question.

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