I'm not sure where I went wrong with my code. Am I using fread and fwrite correctly? Thank you in advance!!

//opens the file
FILE *f = fopen(argv[1], "r");

//if the file contains nothing, return 1 
if (f == NULL)
    printf("Error. Could not open file");
    return 1; 

//variable to keep track of the number of JPEG's opened
int num_jpeg = -1;

//creates a buffer that stores 
BYTE *byee = malloc(512);

//Returns/stores 512 bytes from file
fread(byee, 512, 1, f);

//create char and temporary jpeg file
FILE *img = NULL;

while (fread(byee, 512, 1, f) == 1)
    if (byee[0] == 0xff && byee[1] == 0xd8 && byee[2] == 0xff && (byee[3] & 0xf0) == 0xe0)
        //creates a numbering for the amount of jpeg's opened
        //converts idk into a jpeg file basically 
        char idk[8];    
        sprintf(idk, "%3i.jpg", num_jpeg);
        if (num_jpeg <= 0)
            //opens idk jpeg file 
            img = fopen(idk, "w");
            img = fopen(idk, "w");
        //writes byee into img  
        fwrite(&byee, 512, 1, img);
//frees memory 



You're using it more or less correctly. But there are two problems with the code that was posted. First, it reads the first 512 bytes of data from the input file, but doesn't check it or do anything with it, other than overwriting it with the next read.

Second, once a file has been opened, the data blocks without signatures are not being written out to the output file. You can verify this by checking the size of the output files. They're probably all exactly 512 bytes.

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

You must log in to answer this question.

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