Skip to main content
removed solution code
Source Link
curiouskiwi
  • 18.7k
  • 2
  • 18
  • 43

This is how my loop looks now, what have I done wrong?

// Read each 512 bytes until end of file is reached
FILE* img = NULL;
 
while (fread(&data, 512, 1, diskptr) == 1)
{
    // Check for jpeg start
    if (checkStart(data, jStart, jStartOE) == true)
    {
        jFound++;
        if (jFound == 1)
        {
            printf("newfound\n");
            // Name and open the first jpeg file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        }
        else if (img != NULL)
        {
            printf("closed\n");
            // Close old file
            fclose(img);
            
            // Open new file
            char title[12];
            sprintf(title, "%03d.jpg", jFoundsnipped -by 1);mod
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        } 
    }
    else if (jFound > 0 && img != NULL)
    {
        printf("written\n");
        fwrite(&data, 512, 1, img);
    }
}

I added those printf()s to see if it was ever executing this else ifs and it isn't. newfound is printed out once and then nothing. I only get one corrupt 000.jpg. I don't understand why it's not executing the other lines of code if img is not null anymore because the fact that newfound was printed indicates that the block of code that run fopen() has been executed.

This is how my loop looks now, what have I done wrong?

// Read each 512 bytes until end of file is reached
FILE* img = NULL;
 
while (fread(&data, 512, 1, diskptr) == 1)
{
    // Check for jpeg start
    if (checkStart(data, jStart, jStartOE) == true)
    {
        jFound++;
        if (jFound == 1)
        {
            printf("newfound\n");
            // Name and open the first jpeg file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        }
        else if (img != NULL)
        {
            printf("closed\n");
            // Close old file
            fclose(img);
            
            // Open new file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        } 
    }
    else if (jFound > 0 && img != NULL)
    {
        printf("written\n");
        fwrite(&data, 512, 1, img);
    }
}

I added those printf()s to see if it was ever executing this else ifs and it isn't. newfound is printed out once and then nothing. I only get one corrupt 000.jpg. I don't understand why it's not executing the other lines of code if img is not null anymore because the fact that newfound was printed indicates that the block of code that run fopen() has been executed.

This is how my loop looks now, what have I done wrong?

// Read each 512 bytes until end of file is reached
FILE* img = NULL;
// snipped by mod
            FILE* img = fopen(title, "w");

I added those printf()s to see if it was ever executing this else ifs and it isn't. newfound is printed out once and then nothing. I only get one corrupt 000.jpg. I don't understand why it's not executing the other lines of code if img is not null anymore because the fact that newfound was printed indicates that the block of code that run fopen() has been executed.

Updated while loop
Source Link

This is how my loop looks now, what have I done wrong?

// Read each 512 bytes until end of file is reached
FILE* img = NULL;

while (fread(&data, 512, 1, diskptr) == 1)
{
    // Check for jpeg start
    if (checkStart(data, jStart, jStartOE) == true)
    {
        jFound++;
        if (jFound == 1)
        {
            printf("newfound\n");
            // Name and open the first jpeg file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        }
        else if (img != NULL)
        {
            printf("closed\n");
            // Close old file
            fclose(img);
            
            // Open new file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        } 
    }
    else if (jFound > 10 && img != NULL)
    {
        printf("written\n");
        fwrite(&data, 512, 1, img);
    }
}
fclose(img);
fclose(diskptr);

I added those printf()s to see if it was ever executing this else ifs and it isn't. newfound is printed out once and then nothing. I only get one corrupt 000.jpg. I don't understand why it's not executing the other lines of code if img is not null anymore because the fact that newfound was printed indicates that the block of code that run fopen() has been executed.

This is how my loop looks now, what have I done wrong?

// Read each 512 bytes until end of file is reached
FILE* img = NULL;

while (fread(&data, 512, 1, diskptr) == 1)
{
    // Check for jpeg start
    if (checkStart(data, jStart, jStartOE) == true)
    {
        jFound++;
        if (jFound == 1)
        {
            // Name and open the first jpeg file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        }
        else
        {
            // Close old file
            fclose(img);
            
            // Open new file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        } 
    }
    else if (jFound > 1)
    {
        fwrite(&data, 512, 1, img);
    }
}
fclose(img);
fclose(diskptr);

This is how my loop looks now, what have I done wrong?

// Read each 512 bytes until end of file is reached
FILE* img = NULL;

while (fread(&data, 512, 1, diskptr) == 1)
{
    // Check for jpeg start
    if (checkStart(data, jStart, jStartOE) == true)
    {
        jFound++;
        if (jFound == 1)
        {
            printf("newfound\n");
            // Name and open the first jpeg file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        }
        else if (img != NULL)
        {
            printf("closed\n");
            // Close old file
            fclose(img);
            
            // Open new file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        } 
    }
    else if (jFound > 0 && img != NULL)
    {
        printf("written\n");
        fwrite(&data, 512, 1, img);
    }
}

I added those printf()s to see if it was ever executing this else ifs and it isn't. newfound is printed out once and then nothing. I only get one corrupt 000.jpg. I don't understand why it's not executing the other lines of code if img is not null anymore because the fact that newfound was printed indicates that the block of code that run fopen() has been executed.

Source Link

This is how my loop looks now, what have I done wrong?

// Read each 512 bytes until end of file is reached
FILE* img = NULL;

while (fread(&data, 512, 1, diskptr) == 1)
{
    // Check for jpeg start
    if (checkStart(data, jStart, jStartOE) == true)
    {
        jFound++;
        if (jFound == 1)
        {
            // Name and open the first jpeg file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        }
        else
        {
            // Close old file
            fclose(img);
            
            // Open new file
            char title[12];
            sprintf(title, "%03d.jpg", jFound - 1);
            FILE* img = fopen(title, "w");
            fwrite(&data, 512, 1, img);
        } 
    }
    else if (jFound > 1)
    {
        fwrite(&data, 512, 1, img);
    }
}
fclose(img);
fclose(diskptr);