LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2013, 02:54 AM   #1
salterd
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
why is my file not reading from a jpeg file


Hello all! This is my first time posting to this forum.I am having some serious difficulty reading from a jpeg file.I was told that the file has this format, at the beginning of each signature:
- 0xff 0xd8 0xff 0xe0
- 0xff 0xd8 0xff 0xe1

Now, knowing this, I have put in place this code:
Code:
 typedef unsigned char BYTE ;
    char image_name[8]; 
    int counter=1;
    FILE* fp = fopen("card.raw", "r");
    FILE *outfile=NULL;
    int size=512;
    BYTE buffer[size];
    while (feof(fp) == false) 
    {
        fread(buffer,size,sizeof(unsigned char),fp);
        if (fp== NULL)
        {
            printf("Could not open file \n");
            return 1;
        }
        if(buffer[0]==0xff && buffer[1]==0xd8 &&  buffer[2]==0xff && (buffer [3]==0xff || buffer[3]==0xe1))
        {  
            for(int i=0; i<51; ++i){        
                sprintf(image_name, "%.3d.jpg", counter);        
                outfile= fopen(image_name, "w");
                fwrite(buffer,sizeof(buffer),1,outfile); 
                counter=counter+1;
                if (outfile == NULL)              
                {                   
                    printf("could not create jpeg file\n");                   
                    return 2;              
                }      
            } 
        }
        fclose(fp);
        fclose(&outfile);
        return 0;
    }
NOw, my problem is I cant get the first if statement to return true.I was told the file is indeed legit and is not corrupt(and with certainty it has that signature within its structure). If anyone has any ideas on what could be my problem, I would love to hear them. Thank you all!
 
Old 02-09-2013, 03:44 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Hi,
1)
Code:
if (fp== NULL)
{
    printf("Could not open file \n");
    return 1;
}
This should go before the loop.

2)
Code:
while (feof(fp) == false)
Please note that feof() will only return true after you have attempted to read past the end of the file. So your loop will run one more iteration then you would expect.

3)
Code:
if(buffer[0]==0xff && buffer[1]==0xd8 &&  buffer[2]==0xff && (buffer [3]==0xff || buffer[3]==0xe1))
Notice the bold part. Shouldn't it be 0xe0? Anyway, maybe it would be more readable if you just defined the expected header sequence as an array/string and use something like strncmp() for the comparison.

4) why is the comparison in every iteration? Does the file have a header each 512 bytes?

5)
Code:
                if (outfile == NULL)              
                {                   
                    printf("could not create jpeg file\n");                   
                    return 2;              
                }
Again, you should check this before you attempt to write to the file.

6)
Code:
fclose(&outfile);
why the '&' ?

7) You open a new file in each of the 51 iterations of the inner loop. But you only fclose() once.

8) What is this program supposed to do in the first place?

Last edited by millgates; 02-09-2013 at 03:51 AM.
 
Old 02-09-2013, 02:34 PM   #3
salterd
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
Hi,
1)
Code:
if (fp== NULL)
{
    printf("Could not open file \n");
    return 1;
}
This should go before the loop.

2)
Code:
while (feof(fp) == false)
Please note that feof() will only return true after you have attempted to read past the end of the file. So your loop will run one more iteration then you would expect.

3)
Code:
if(buffer[0]==0xff && buffer[1]==0xd8 &&  buffer[2]==0xff && (buffer [3]==0xff || buffer[3]==0xe1))
Notice the bold part. Shouldn't it be 0xe0? Anyway, maybe it would be more readable if you just defined the expected header sequence as an array/string and use something like strncmp() for the comparison.

4) why is the comparison in every iteration? Does the file have a header each 512 bytes?

5)
Code:
                if (outfile == NULL)              
                {                   
                    printf("could not create jpeg file\n");                   
                    return 2;              
                }
Again, you should check this before you attempt to write to the file.

6)
Code:
fclose(&outfile);
why the '&' ?

7) You open a new file in each of the 51 iterations of the inner loop. But you only fclose() once.

8) What is this program supposed to do in the first place?
Yes I do open a new file! The premise of the program is to read in 512 bytes at a time.For each 512 bytes, there will be a new image and there are 51 images within this file. I'm not worried about that part so much as I'm worried on why I cant get the first if statement to return true...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading ext3 file system on Mac and overriding file permission moravveji Other *NIX 6 09-12-2011 02:25 AM
[SOLVED] retrieving jpeg files from BkUp copy yields:ERR INTERPRETING JPEG; file not jpeg, drmjh Linux - General 7 10-03-2010 08:13 AM
[SOLVED] Reading in a file in C, after file read null terminator at strings end gone josip76 Programming 2 05-14-2010 12:44 AM
Reading .jpeg / .gif / or any image file in C in Linux amroz Linux - Newbie 1 06-03-2008 10:33 AM
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration