Keep getting "Can't open card.raw: file format unsupported" error when running the code. Not sure what I'm doing wrong.

#include <stdio.h>
#include <stdlib.h>

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

    if (argc!=2){
        printf("Usage: ./recover image");
        return 1;

    FILE *fptr;
    FILE *img;

    fptr = fopen("card.raw", "r");
    if(fptr == NULL){
    _uint8_t buffer[512];
    char filename[8];
    int filecount=0;

    while(fread(buffer, 1, 512,fptr)>0){

        if(buffer[0]== 0xff && buffer[1] == 0xd8 && buffer [2] == 0xff && (buffer[3] & 0xf0) == 0xe0){
            img = fopen(filename,"a");
            fwrite(buffer, 1, 512, img);
            fread(buffer, 1, 512, fptr);

            while (buffer[0]!= 0xff || buffer[1] != 0xd8 || buffer [2] != 0xff || (buffer[3] & 0xf0) != 0xe0){
                fwrite(buffer, 1, 512, img);

The first problem I see is that "card.raw" is hardcoded. The spec says to use that the first input parameter should be the name of the input file, not that it should be hardcoded. But that's not your problem here.

My best guess is that the card.raw file is corrupted somehow. You should redownload it and replace it.

If that isn't the problem, my next question would be whether you're working with an executable compiled from a source file different from the one posted. Or, the card.raw is somehow locked because it's open somewhere else and is perhaps locked by an editor or other program.

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 .