0

While debugging, the compiler doesn't enter the condition (although it should be entered at the first iteration), also I get segmentation fault in the fwrite line after the conidion

I 'm including only the effective part of the code

    // creating the buffer
    unsigned char buffer[512];

    // char for the write
    char *writeFile = NULL;

    // reading the file
    while((fread(buffer, 512, 1, readFile)) != 0)
    {
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // incrementing for name
            i++;

            // naming
            sprintf(writeFile, "%03i.jpeg", i);

            //opening the file for writing
            FILE *output = fopen(writeFile, "w");

            // writing into the file
            fwrite(buffer, sizeof(buffer), 1, output);

            // closing the files
            fclose(output);

            continue;
        }

        //opening the file for writing
        FILE *output = fopen(writeFile, "w");

        // writing into the file
        fwrite(buffer, sizeof(buffer), 1, output);

        // closing the files
        fclose(output);
    }

    fclose(readFile);
}

Thanks in advance

1

I'm thinking you got a little confused when you were writing this part. Note that I added comments, "LINE A" and "LINE B":

// char for the write
    char writeFile[count * sizeof(buffer)];   // LINE A

    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        //opening the file for writing
        FILE *output = fopen(writeFile, "w");   // LINE B

Let's look at what happens in this code. Line B attempts to open the output file, but it's more or less failing. the fopen() call uses the string in writefile as the filename to open. But what's in writefile? Let's go back to LINE A.

In LINE A, the char array variable writefile is declared, and it's declared to have a size of count * 512. Why? You already have buffer to hold each 512 bytes read in from the input file. That's an awful lot of space to use for a text string with, at most, 7 chars plus the end of string marker!

Also, and more relevant, the filename of the output file is never put into the writefile string (char array)! So, when it creates a filename, it uses whatever junk is in the array. I'll wager that you have some strangely named files in your IDE. You should do some cleanup and delete them while fresh on your mind.

The bottom line is that this code is going to have some very unpredictable behavior in creating files.

It looks like you've conflated the use of writefile to hold the filename with holding the buffer contents. The code isn't creating files with the correct file names, so it's failing when check50 runs it. BTW, did you test it yourself before running check50??? Didn't you notice that there were no output files with correct names????

Here's another hint. Declare the output file pointer before the start of the while loop, but set it to NULL at that point. Call fopen() later, inside the for loop as needed.

There's more code to be written, so this will get you going. Happy coding!

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

6
  • Thanks Cliff B for your time and help, it really matters. Oct 20 '21 at 8:37
  • I 've edited the code and compiled it, and the current problem is that the condition is not entered and I get segmentation fault. I 've edited the question putting the up-to-date code. Oct 20 '21 at 8:38
  • Hello, problem solved after making MAJOR changes to code, Thanks. Oct 25 '21 at 11:07
  • But there is a question, in the code I wrote a condition to write if i>0, but it failed to recover the last image until I added && !feof(outFile) why is that? although the outer while loop had !feof(outFile), hope I could illustrate the question :) Oct 25 '21 at 11:10
  • I'd have to see it in context. Suggest you ask on one of the interactive forums where you can share code one on one.
    – Cliff B
    Oct 25 '21 at 13:13

You must log in to answer this question.

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