0

Not actually sure what is wrong with my code but it doesn't end when it's executed. I tried adding return 0 so it would end but it's not working.

code:

#include <stdio.h>
#include <stdint.h>

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
char *f_name;
int image_count = 0;
if (argc != 2)
{
    printf("Usage: ./img\n");
    return 1;
}

//input file
FILE *source = fopen(argv[1], "r");
if (source == NULL)
{
    return 1;
}

//output file
FILE *destination = fopen(f_name, "w");
destination = NULL;

//buffer
BYTE buffer[512];

//check  for jpeg
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
    while(fread(buffer, sizeof(BYTE), 512, source))
    {
        image_count ++;
        if (destination != NULL)
        {
            fclose(destination);
        }
        fwrite(buffer, sizeof(BYTE), 512, destination);
        sprintf(destination, "%i", f_count);
    }
}
fclose(source);
fclose(destination);
return 0;
}
2

The main problem is in the DO-WHILE loop. Let's see your program from reading in the image file. The fread() function reads a block from f and stores it in b (more descriptive names would be useful, this is not pure mathematics either), then we enter the loop, which is executed at least once (without checking the end condition Cycle). Inside the loop there is an IF condition, if you ask yourself if the condition is true, the answer is that it is not, with which we arrive at the end of loop condition (while (! feof (f)); // while not end of file ), since we are not at the end of the condition is true, and we enter the cycle again. And this is where the real problem of the code comes into play. The values b [0], b [1], etc ... have not changed, they are still the same, with which we have an INFINITE LOOP. The fread() function does not run more than once. I suggest you remove your do-while loop and the fread() function as it is, and replace it with the following:

while(fread(b, sizeof(unsigned char), 512, f))
2
  • Thank you, I'll try this. – Gloria Isedu Dec 15 '20 at 21:32
  • I've modifies the code and renamed the variables so they are more readable. And I tried running the code(it compiles fine) but it give a seg fault. – Gloria Isedu Feb 28 at 0:16
0

There are several issues. First, get used to correct indentation. I believe the IDE helps with this. Simply put the cursor at the beginning of the line, and "tab" will insert the correct number of spaces for that line. This may sound picky, but once you get more experienced, it will help you read your own code. And, it helps anyone looking at your code.
It seems you're opening the destination, then setting it to NULL. This can get removed, and could be the source of the problem.
Later, you're checking if destination is not NULL, and closing it. You'll need to open a new destination before writing to it. Otherwise the segfault will recur.
Once you have the *destination set up, no mg faults, you should look at the relative placement of the while loop which contains the fread, and the if (buffer...) lines. When does the buffer array get populated?
One more bit of advice: if you make progress,and would like help with a new issue, open a new question.

1
  • The indentation gets messed up when I'm trying to get the code to not look like text on stackexchange. but thank you – Gloria Isedu Mar 1 at 0:10

You must log in to answer this question.

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