I'm very confused why this code is not giving any output: Code:
#include <stdio.h> #include <stdlib.h> #include <stdint.h> typedef uint8_t BYTE; long int findSize(char file_name[]) { // opening the file in read mode FILE* fp = fopen(file_name, "r"); // checking if the file exist or not if (fp == NULL) { printf("File Not Found!\n"); return -1; } fseek(fp, 0L, SEEK_END); // calculating the size of the file long int res = ftell(fp); // closing the file fclose(fp); return res; } void copy(BYTE newarr[], BYTE oldarr[]) { for (int i = 0; i < 512; i++) { newarr[i] = oldarr[i]; } } int main(int argc, char *argv[]) { if (argc < 2 || argc > 2) { printf("Usage: ./recover image"); return 1; } FILE *card = fopen(argv[1], "r"); if (card == NULL) { printf("Unable to open: %s\n", argv[1]); return 2; } else { BYTE buffer[512]; int file_size = findSize(argv[1])/512; BYTE block[file_size][512]; int block_counter = 0; while (fread(&buffer, sizeof(BYTE), 512, card)) { copy(block[block_counter], buffer); } int file_count = 0; char filename[17]; FILE *out = NULL; for (int block_number = 0; block_number < file_size; block_number++) { if (block[block_number][0] == 0xff && block[block_number][1] == 0xd8 && block[block_number][2] == 0xff && (block[block_number][3] & 0xf0) == 0xe0) { if (file_count > 0) { fclose(out); } sprintf(filename, "%03i.jpg",file_count); out = fopen(filename, "w"); fwrite(&block[block_number], sizeof(BYTE), 512, out); file_count++; } else if (file_count > 1) { fwrite(&block[block_number], sizeof(BYTE), 512, out); } } if (out != NULL) { fclose(out); } } if (card != NULL) { fclose(card); } return 0; }