-1

I have been trying to solve this question for a while now but I am still not sure what is wrong. The program works fine before the while loop but after it stops working and when checking with check50 it says that there are segmentation faults.

enter image description here

My code also makes a blank file with a question mark.

enter image description here

This is the code that I made:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    // checks if the right things were writen
    if (argc < 2)
    {
        printf("Write: ./recover card.raw \n");
        return 1;
    }
    // stores file info
    BYTE buffer[512];
    // keeps trak of the num of jpgs
    int jpgnum = 0;
    // name of jpgs
    char name[8];
    // memory card
    FILE *file = fopen(argv[1], "r");
    // checks if there is forensic image
    if (file == NULL)
    {
        printf("Can't open memory \n");
        return 1;
    }
    // the img
    FILE *img = NULL;

    // CODE WORKS HERE

    // repeats until end of card
    while (fread(buffer, sizeof(BYTE), 512, file) == 512)
    {

        // CODE DOESN'T WORK HERE

        // checks if is starting of new img
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // if it is NOT the first img then close previous image
            if (file != NULL)
            {
                fclose(img);
            }
            // set and write to the img
            sprintf(name, "%03i.jpg", jpgnum);
            img = fopen(name, "w");
            fwrite(buffer, 1, 512, img);
            // increase jpgnum by 1
            jpgnum++;
        }
        else
        {
            // if not starting then continue writing
            if (file != NULL)
            {
                fwrite(file, 1, 512, img);
            }
        }
    }
    // close files
    fclose(file);
    fclose(img);
}

I would appreciate it if someone could help me solve the problem.

3
  • step through a debugger. what line is it segfaulting on? does name get the string value you expect it to? Also note that you are not checking if the write or the open of the file for writing actually succeeded. you probably want to do that. Try first seeing if without writing the files to disk you can even printout the correct 50 names of the files you would write... then try actually opening the files for writing... and then try actually writing the data into them
    – UpAndAdam
    Sep 18, 2023 at 19:40
  • also please dont provide us with images for your error messages, provide the actual error messages, many of us are behind firewalls and can't always see your images.
    – UpAndAdam
    Sep 18, 2023 at 19:41
  • Actually, this was a properly formatted question as displayed when I made this comment. The code itself is pasted in. That's the big thing. Using images of the IDE to show the phantom file is good too, to show exactly what's listed there. The check50 results could go either way, but would be better to cut and paste so people could copy and paste the link to the expanded cs50 result. In a word, it's fine as it is.
    – Cliff B
    Sep 22, 2023 at 4:16

1 Answer 1

0

These are the kind of errors that drive programmers insane!!! Here's a really big hint.

WHICH files are being checked to see if the pointer is NULL, the output file or the input file? ;-)

There are other issues, but you haven't had a chance to work on them. ;-)

You should practice using the debugger as well as valgrind. Debugger proficiency is simply one of your most important tools. Valgrind will usually tell you which line generated the seg fault.

As for the oddly named file, I suspect it was created by execution of an earlier version of the program that didn't properly create the expected output file names and was left undeleted. Just delete it.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • This probably will help my code a lot so thank you a lot but there's another issue where the code skips the while loop entirely. If there is a way to fix that I would like to know.
    – Raheel A
    Sep 24, 2023 at 16:41
  • That would be for a new question. ;-)
    – Cliff B
    Sep 25, 2023 at 1:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .