0

My version of Recover for pset3 is creating 50 .jpg files but only as textfiles not images and I can't work out why. I've looked through this forum and saw one similar question but the issue identified (using "Filename" for the fopen command) doesn't apply to me. I've also just gone through the IDE shift so downloaded my .tar file from the c9.io and uploaded it to cs50.io and when I ran the script in the previous IDE it actually created the picture files so I'm wondering if either the IDE change has caused an issue or I've accidnetally changed something while restarting my work on this. In spirit of academic honesty, I've tried to only copy the relevant parts of my code below (although it is fairly extensive unfortunately as I'm not sure where the error is coming from).

Really appreciate any assistance with this - thought I was getting pretty close and was getting the images out and now I seem to have gone backwards!

    //Create 8 character string for output files
char image [8];

//Create output file as Null
FILE *outptr = NULL;

//Initialize counter for output images
int counter = 0;

//While statement to end when block is less than 512 to mark end of file
while ( fread(block, sizeof(BYTE), sizeof(block), inptr) == 512)
{
    //If statement to check if JPG header on each block and increase counter if header found
    if ( block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
    {

        //If not first file, close existing image file first
        if (counter > 0)
        {
            fclose(outptr);
        }

        //Open up new output file
        sprintf(image, "%03i.jpg\n", counter);
        outptr = fopen(image, "w");
        if (outptr == NULL)
        {
            fprintf(stderr, "Could not open %s.\n", image);
            return 2;
        }
    fwrite(&block, sizeof(BYTE), sizeof(block), outptr);
    counter++;

    }

    //If JPG open, then write block to the output file
    else if (counter > 0)
    {
    fwrite(&block, sizeof(BYTE), sizeof(block), outptr);
    }
  • You can start by removing the "\n" line feed from the filename. If there's more, I'll look after I get some sleep. ;-) – Cliff B Jul 7 '19 at 14:54
  • Thanks very much, will try that when next on! Definitely not urgent enough to disrupt sleep :) Realised I had mistakenly put this as Pset4 (being optimistic about my progress!) so have corrected title and tag to Pset3 – Nealo888 Jul 8 '19 at 6:16
  • Thanks very much Cliff, that solved the issue and code worked well after that, much appreciated! – Nealo888 Jul 9 '19 at 0:43
0

Asked and answered in comments. Please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You can start by removing the "\n" line feed from the filename. If there's more, I'll look after I get some sleep. ;-) – Cliff B yesterday

Thanks very much, will try that when next on! Definitely not urgent enough to disrupt sleep :) Realised I had mistakenly put this as Pset4 (being optimistic about my progress!) so have corrected title and tag to Pset3 – Nealo888 yesterday

Thanks very much Cliff, that solved the issue and code worked well after that, much appreciated! – Nealo888 5 hours ago

| improve this answer | |

You must log in to answer this question.

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