0

For some reason my program is not wanting to open card.raw. I have tried rearranging variables and I just don't see where I am going wrong. Any help would be appreciated!

uint8_t buffer[512];
char titlez[8];


int main(void)
{
    FILE* inptr = fopen("card.raw", "r");

    //if the file cannot be opened we yell at the user
    if (inptr == NULL);
    {
        printf("Unable to read card.raw\n");
        return 1;
    }

    //lets create our outfile
    FILE* outptr = fopen(titlez, "w");

    //we want to repeat the following lines of code until the end of the card is reached.
    while(fread(&buffer, sizeof(uint8_t), 512, inptr) == 512)
    {
        //we want to read 512 bytes at a time into our buffer
        fread(&buffer, 512, 1, inptr);

        //lets create a counter to store the images
        int imgnumz=0;
        int alreadyFound=0;

        //lets check to see if we are at the beginning of a jpg
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            //we want to create an outputfile
            outptr = fopen(titlez, "w");

            //lets store our jpeg filename
            sprintf(titlez, "%03d.jpg", imgnumz);

            //we want to check for our first image
            if(alreadyFound == 0)
            {

                //lets write from our buffer into our output file 512 bytes at a time
                fwrite(buffer, sizeof(uint8_t), 1, outptr);

                //we need to update to already found and increase our img number
                alreadyFound++;
                imgnumz++;  
            }

            //for imgs after our first
            else if (alreadyFound > 0)
            {
                fclose(outptr);
                outptr = fopen(titlez, "w");
                fwrite(buffer, sizeof(uint8_t), 1, outptr);
                imgnumz++;
            }

        }

        else
        {
            fwrite(buffer, sizeof(uint8_t), 1, outptr);
        }

    }
    if(outptr)
    {
        fclose(outptr);
    }
    return 0;   
}
0

When you run your program, is the terminal displaying "unable to read card.raw"? I'm seeing a possible problem with your output file, in that you are opening the file for writing, but the file has no name, or a garbage value for a name, because your variable titlez is not initialized.

4
  • Yes it says unable to read card.raw. How should I initialize the variable with malloc? – Jose A. Alvarez May 20 '15 at 13:12
  • I found my error. – Jose A. Alvarez May 20 '15 at 19:33
  • What was the error and how did you resolve it? It may help a student in the future. – KernelRutgers May 20 '15 at 20:19
  • Probably wrong directory, or the file was moved/deleted. – i_am_david Jul 20 '15 at 16:18

You must log in to answer this question.

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