0

Whenever I run my code using check50, I get this error: "timed out while waiting for program to exit", and I don't know why this is happening. Any help would be greatly appreciated. Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{

    //check for proper usage
    if (argc != 2)
    {
        printf("Usage: ./recover image");
        return 1;
    }

    //initialization
    char filename[8] = {'#', '#', '#', '.', 'j', 'p', 'g'};

    //open memory card
    FILE *mc = fopen(argv[1], "r");

    if (mc == NULL)
    {
        //memory card doesn't exist
        return 1;
    }

    //initialization of number of jpegs
    int nofjpegs = 0;

    bool firstjpeg = true;

    unsigned char storagearray[512];

    //read memory card
    fread(storagearray, 512, 1, mc);

    while (!feof(mc))
    {
        //check for the start of a jpeg
        if (storagearray[0] == 0xff && storagearray[1] == 0xd8 && storagearray[2] == 0xff && (storagearray[3] & 0xf0) == 0xe0)
        {
            if (firstjpeg == true)
            {
                sprintf(filename, "%03i.jpg", nofjpegs);
                nofjpegs++;
                firstjpeg = false;
            }
            else
            {
                fclose(img);
                sprintf(filename, "%03i.jpg", nofjpegs);
                nofjpegs++;
            }

            FILE *img = fopen(filename, "w");
            fwrite(storagearray, 512, 1, img);
        }
    }


    fclose(img);
    fclose(mc);

    return 0;
}
1

I am not a professional just a student.

What I am seeing is your program logic is not right.

First of all your FOR loop is not right! better use while loop.

bool endFile = false;

while(!endFile)
{
    //Check if end-of-file
    if(feof(mc))
    {
        endFile = true;
    }
}

On the line 34: You are initializing file to null? Change it to: FILE *img; But there is no need to do this you can initialize file at the same time while opening or writing to it. You can do this on line 62: FILE *img = fopen(filename, "w");.

Your if statement is wrong? for me it is.

if (i > 2 && storagearray[i - 3] == 0xff && storagearray[i - 2] == 0xd8 && storagearray[i - 1] == 0xff && (storagearray[i] & 0xf0) == 0xe0)

you are declaring BYTE i = 0? why initializing to BYTE there while you are just using it as int? and storagearray[i - 3]? => Behind program it is like this 0 - 3? I can't understand your if statement!

Better is this:

if (storagearray[0] == 0xff && storagearray[1] == 0xd8 && storagearray[2] == 0xff && (storagearray[3] & 0xf0) == 0xe0)

I can't tell you every bit of code as it will not help you to learn now you need to do is come up with better logic this is not right one.

  • Use While loop.
  • Initialize storage array as BYTE instead of unsigned char!
  • Use close files after using file correctly.
  • Use better logical if statements.
5
  • I mostly followed your advice, but I used while (!feof(mc)) {} instead for the while loop. But still, the code's not working. Same problem as before. It gets timed out – Areen Mar 28 '20 at 16:41
  • I updated my code, which is displayed above. Please help me find the source of the error. Thank you for your answer. – Areen Mar 28 '20 at 17:40
  • Your program cant go further as you have told program to check if first block is jpeg and wrote all code if the first block is JPEG what if its not what the program does then? that's why it stays forever in while loop or for loop and can't execute further which makes your program time out. – Azha Balouch Mar 28 '20 at 17:41
  • you need else in first if statement of while loop – Azha Balouch Mar 28 '20 at 17:43
  • Your program is only exec for the first block of memory your program is not reading next block of memory of file as you have not specified it. You need fread(storagearray, 512, 1, mc); more than once to read more data. – Azha Balouch Mar 28 '20 at 18:20
0

I solved it.

For the fread statement, I used: size_t bytesRead = fread(storagearray, sizeof(BYTE), 512, mc);.

I also used an infinite loop and I used the following to break out of the loop:

//breaks out of the loop when the end of a card image is reached
        if (bytesRead == 0 && feof(mc))
        {
            break;
        }

I used an approach to solve the problem like the one here: https://github.com/mareksuscak/cs50/blob/master/pset4/recover/recover.c

You must log in to answer this question.

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