When I try to run my solution to recover in cs50 problem set 4, I get a segmentation fault. I tried to use check50, and I got a poor response for memory leaks.
:( program is free of memory errors
According to the log, this occurred on line 44. Line 44 being:
output = fopen(name, "w");
Does anyone understand why I might be getting an error such as this one?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
// Check for more more or less than 2 arguements
if (argc != 2)
{
printf("Usage: ./recover IMAGE\n");
return 1;
}
FILE *input = fopen(argv[1], "r");
// Check if file is accessibl
if (input == NULL)
{
printf("Could not open file\n");
return 2;
}
// Create the output file, buffer, jgp counter, and file name tracker
FILE *output = NULL;
BYTE buffer[512];
int jpgCount = 0;
// 3 for chars, 4 for int, and 1 for nul char
char name[8];
// Read until end of file. This while loop stops when fread equals zero, because that is when it has nothing left to find
while (fread(buffer, sizeof(BYTE), 512, input) != 0)
{
// Check if there is a jpg
// Be wary of this code, when I wrote this, it had many memory leaks
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff)
{
//Name the file
sprintf(name, "%03i.jpg", jpgCount);
jpgCount++;
// Open the new file
output = fopen(name, "w");
}
// Write information to the file ONLY if it is not blank, otherwise it gives a segmentation fault
if (output != NULL)
{
fwrite(buffer, sizeof(BYTE), 512, output);
}
}
// Close all files
fclose(output);
fclose(input);
}