When I run my code in debug mode it works fine and creates 50 jpg images. When I run it like ./recover card.raw
it waits forever without an error code. When I submit it, it times out. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
// check command line args
if (argc != 2) {
printf("Usage: ./recover file.raw\n");
return 1;
}
// define variables
typedef uint8_t BYTE;
BYTE buffer[512];
int readBytes = 0;
int blockNum = 0;
int numOfImages = 0;
// open card and open a new file for saving jpeg
FILE *f = fopen(argv[1], "r");
FILE *pNewImage;
// loot through the file and repeat this until the end of a file is reached
do {
blockNum++;
readBytes = fread(buffer, sizeof(BYTE), 512, f);
// if a block is a start of a new JPEG
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3] & 0xf0) == 0xe0)) {
numOfImages++;
char filename[8];
if (numOfImages == 1) { // Save buffer to a new file if it's a first image
sprintf(filename, "%03i.jpg", numOfImages - 1); // write to variable filename like 001.jpg
pNewImage = fopen(filename, "w");
fwrite(buffer, sizeof(BYTE), 512, pNewImage);
} else { // close image file if it's open from last save and save it to a new file
if (pNewImage != NULL) {
fclose(pNewImage);
}
sprintf(filename, "%03i.jpg", numOfImages - 1);
pNewImage = fopen(filename, "w"); // Open a file
fwrite(buffer, sizeof(BYTE), 512, pNewImage);
}
printf("Found beginning of an image in a block %i\n", blockNum);
} else { // if a block is not a start of a new image
if (pNewImage != NULL) { // If it's open write, else ignore
fwrite(buffer, sizeof(BYTE), 512, pNewImage);
} else {
continue;
}
}
} while (readBytes == 512);
fclose(pNewImage);
fclose(f);
return 0;
}
In debugger, it creates 50 jpg images and returns fine as you can see in the picture.