0

It compiles. But if I step into this function: fread(&pos, sizeof(BYTE), 1, file);

I get an error from GDB: _GI_IO_fread (buf=0xbfffff0d, size=1, count=1, fp=0x804b008) at iofread.c:35 iofread.c: No such file or directory.

It keeps saying: in iofread.c with different line numbers.

How can I read the infile? What am I doing wrong?

include

include

include "bmp.h"

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

{ // remember file name

char* infile = "card.raw";

// open CF card
FILE* file = fopen(infile, "r");
if (file == NULL)
{
    printf("Could not open %s.\n", file);
    fclose(file);
    return 1;
}

// create storage for position
fpos_t pos;

// get current position
fgetpos(file, &pos);

// counter for the recovered photo's
int i = 000;


// read until JPEG is detected.
while (fgetpos(file, &pos) != EOF)
{
    // read 1 byte
    fread(&pos, sizeof(BYTE), 1, file);

    // search
    if (fgetpos(file, &pos) == 0xff)
    {
        // read 1 byte and adjust position
        fread(&pos, sizeof(BYTE), 1, file);
        fsetpos(file, &pos);

        if ((fgetpos(file, &pos) + 1  == 0xd8) && (fgetpos(file, &pos) + 2 == 0xff) 
        && (fgetpos(file, &pos) + 3 == 0xe0 || fgetpos(file, &pos) + 3 == 0xe1))
        {              
            // create and open a JPEG
            FILE* photo = fopen("[i].jpg", "w");
            photo = malloc(513 * (sizeof(BYTE)));

            // copy if malloc isn't NULL
            if (photo != NULL)
            {                
                // copy 512 bytes
                for (int pixel = 0; pixel < 511; pixel++)
                {
                    fread(&pos, sizeof(BYTE), 1, file);
                    fwrite(&pos, sizeof(BYTE), 1, photo);
                    fsetpos(file, &pos);
                }

                // close JPEG & adjust counter
                free(photo);
                fclose(photo);
                i++;
            }
        }
    }
  //  TODO: check if search continues
  else 
  {
      fsetpos(file, &pos);
  }
}

// close card-file
fclose(file);

}

1 Answer 1

0

You're using the same variable pos to retrieve the position in the file and to store the contents read from it, a single byte of data. That just wreaks havoc with your program!

More importantly, why are you trying to read in a single byte at a time from the input file instead of reading in 512 bytes at the same time? Same question for the output writes. Reads and writes to a hard drive are incredibly inefficient when compared to memory reads and writes, maybe a thousand times slower. You are doing 512 individual reads or writes for every data block when you could be doing just one.

There are other issues with your code that you will discover as you progress. Consider also that there is no need for fgetpos(), since fread() and fwrite() will handle updating the position in the file pointer automatically. You should really go back and study the pset assignment, along with fread() and fwrite() and then rethink your entire program.

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

You must log in to answer this question.

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