#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
const int BLOCK = 512;
void write_header(FILE *infile, char *file_name, int *file_count, BYTE *buffer);
int main(int argc, char *argv[])
{
//only proceed if correct number of command line arguments
if (argc != 2)
{
printf("Usage: ./recover file.raw\n");
return 1;
}
//open memory card and check not null
FILE *pinput = fopen(argv[1], "r");
if (pinput == NULL)
{
printf("Could not open file.\n");
return 2;
}
//create buffer of 512 bytes
BYTE buffer[BLOCK];
int file_count = 0;
//8 byte array of characters for 000.jpg\0
char *filename = malloc(sizeof(char) * 8);
filename = NULL;
FILE *poutput = NULL;
while (fread(buffer, BLOCK, 1, pinput))
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//if 1st jpeg (only difference is not to close a file)
if (file_count == 0)
{
sprintf(filename, "%03i.jpg", file_count);
poutput = fopen(filename, "w");
fwrite(buffer, BLOCK, 1, poutput);
file_count++;
}
else
{
fclose(poutput);
write_header(poutput, filename, &file_count, buffer);
}
}
//dont do this if 1st one
else if (file_count > 0)
{
fwrite(buffer, BLOCK, 1, poutput);
}
}
//close remaining files and free up malloc
free(filename);
fclose(pinput);
fclose(poutput);
return 0;
}
void write_header(FILE *infile, char *file_name, int *file_count, BYTE *buffer)
{
sprintf(file_name, "%03i.jpg", *file_count);
infile = fopen(file_name, "w");
fwrite(buffer, BLOCK, 1, infile);
*file_count = *file_count + 1;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
const int BLOCK = 512;
void write_header(FILE *infile, char *file_name, int *file_count, BYTE *buffer);
int main(int argc, char *argv[])
{
//only proceed if correct number of command line arguments
if (argc != 2)
{
printf("Usage: ./recover file.raw\n");
return 1;
}
//open memory card and check not null
FILE *pinput = fopen(argv[1], "r");
if (pinput == NULL)
{
printf("Could not open file.\n");
return 2;
}
//create buffer of 512 bytes
BYTE buffer[BLOCK];
int file_count = 0;
//8 byte array of characters for 000.jpg\0
char *filename = malloc(sizeof(char) * 8);
filename = NULL;
FILE *poutput = NULL;
while (fread(buffer, BLOCK, 1, pinput))
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//if 1st jpeg (only difference is not to close a file)
if (file_count == 0)
{
sprintf(filename, "%03i.jpg", file_count);
poutput = fopen(filename, "w");
fwrite(buffer, BLOCK, 1, poutput);
file_count++;
}
else
{
fclose(poutput);
write_header(poutput, filename, &file_count, buffer);
}
}
//dont do this if 1st one
else if (file_count > 0)
{
fwrite(buffer, BLOCK, 1, poutput);
}
}
//close remaining files and free up malloc
free(filename);
fclose(pinput);
fclose(poutput);
return 0;
}
void write_header(FILE *infile, char *file_name, int *file_count, BYTE *buffer)
{
sprintf(file_name, "%03i.jpg", *file_count);
infile = fopen(file_name, "w");
fwrite(buffer, BLOCK, 1, infile);
*file_count = *file_count + 1;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
const int BLOCK = 512;
void write_header(FILE *infile, char *file_name, int *file_count, BYTE *buffer);
int main(int argc, char *argv[])
{
//only proceed if correct number of command line arguments
if (argc != 2)
{
printf("Usage: ./recover file.raw\n");
return 1;
}
//open memory card and check not null
FILE *pinput = fopen(argv[1], "r");
if (pinput == NULL)
{
printf("Could not open file.\n");
return 2;
}
//create buffer of 512 bytes
BYTE buffer[BLOCK];
int file_count = 0;
//8 byte array of characters for 000.jpg\0
char *filename = malloc(sizeof(char) * 8);
FILE *poutput = NULL;
while (fread(buffer, BLOCK, 1, pinput))
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//if 1st jpeg (only difference is not to close a file)
if (file_count == 0)
{
sprintf(filename, "%03i.jpg", file_count);
poutput = fopen(filename, "w");
fwrite(buffer, BLOCK, 1, poutput);
file_count++;
}
else
{
fclose(poutput);
write_header(poutput, filename, &file_count, buffer);
}
}
//dont do this if 1st one
else if (file_count > 0)
{
fwrite(buffer, BLOCK, 1, poutput);
}
}
//close remaining files and free up malloc
free(filename);
fclose(pinput);
fclose(poutput);
return 0;
}
void write_header(FILE *infile, char *file_name, int *file_count, BYTE *buffer)
{
sprintf(file_name, "%03i.jpg", *file_count);
infile = fopen(file_name, "w");
fwrite(buffer, BLOCK, 1, infile);
*file_count = *file_count + 1;
}
pset4 recover Segmentation Fault jpg000
Please can someone explain why I get a segmentation fault?
The code below works for check50 but ideally I'd like to use the function write_header to write the first file.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
const int BLOCK = 512;
void write_header(FILE *infile, char *file_name, int *file_count, BYTE *buffer);
int main(int argc, char *argv[])
{
//only proceed if correct number of command line arguments
if (argc != 2)
{
printf("Usage: ./recover file.raw\n");
return 1;
}
//open memory card and check not null
FILE *pinput = fopen(argv[1], "r");
if (pinput == NULL)
{
printf("Could not open file.\n");
return 2;
}
//create buffer of 512 bytes
BYTE buffer[BLOCK];
int file_count = 0;
//8 byte array of characters for 000.jpg\0
char *filename = malloc(sizeof(char) * 8);
filename = NULL;
FILE *poutput = NULL;
while (fread(buffer, BLOCK, 1, pinput))
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//if 1st jpeg (only difference is not to close a file)
if (file_count == 0)
{
sprintf(filename, "%03i.jpg", file_count);
poutput = fopen(filename, "w");
fwrite(buffer, BLOCK, 1, poutput);
file_count++;
}
else
{
fclose(poutput);
write_header(poutput, filename, &file_count, buffer);
}
}
//dont do this if 1st one
else if (file_count > 0)
{
fwrite(buffer, BLOCK, 1, poutput);
}
}
//close remaining files and free up malloc
free(filename);
fclose(pinput);
fclose(poutput);
return 0;
}
void write_header(FILE *infile, char *file_name, int *file_count, BYTE *buffer)
{
sprintf(file_name, "%03i.jpg", *file_count);
infile = fopen(file_name, "w");
fwrite(buffer, BLOCK, 1, infile);
*file_count = *file_count + 1;
}
If I replace this:
while (fread(buffer, BLOCK, 1, pinput))
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//if 1st jpeg (only difference is not to close a file)
if (file_count == 0)
{
sprintf(filename, "%03i.jpg", file_count);
poutput = fopen(filename, "w");
fwrite(buffer, BLOCK, 1, poutput);
file_count++;
}
else
{
fclose(poutput);
write_header(poutput, filename, &file_count, buffer);
}
}
//dont do this if 1st one
else if (file_count > 0)
{
fwrite(buffer, BLOCK, 1, poutput);
}
}
with this:
while (fread(buffer, BLOCK, 1, pinput))
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//if 1st jpeg (only difference is not to close a file)
if (file_count == 0)
{
write_header(poutput, filename, &file_count, buffer);
}
else
{
fclose(poutput);
write_header(poutput, filename, &file_count, buffer);
}
}
//dont do this if 1st one
else if (file_count > 0)
{
fwrite(buffer, BLOCK, 1, poutput);
}
}
I get a segmentation fault and can't work out why. Please help!
Using debug50 it seems that the fault occurs on this line for would-be file 000.jpg:
else if (file_count > 0)
{
fwrite(buffer, BLOCK, 1, poutput);
}
Thanks in advance!
default