0

so every time I run my program I get the error message: "unsupported file format". What's more, even if I delete the line handling the file format, the program still fails due to "File size limit exceeded". Any tips please?

Hopefully the code isn't too messy, I tried to keep it commented.

int main(int argc, char* argv[])
{
    if (argc != 4)
    {
        printf("Proper usage: n, infile, outfile\n");
        return 1;
    }
    char* infile = argv[2];
    char* outfile = argv[3];
    int n = atoi(argv[1]);

    //open infile
    FILE* inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        printf("Could not open %s.\n", infile);
        return 2;
    }

    //open outfile
    FILE* outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }

    //read infile BFHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    //read outfile BIHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    //save for future purpose
    int width = bi.biWidth;
    int height = bi.biHeight;

    //edit the headers
    bf.bfSize = 54 + bi.biSizeImage * n;
    bi.biSizeImage = bi.biSizeImage * n;
    bi.biWidth = bi.biWidth * n;
    bi.biHeight = bi.biHeight * n;

    //write in the updated BFHEADER
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
    //write in the updated BIHEADER
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

    //determine padding
    int padding = (4 - (width * sizeof(RGBTRIPLE) % 4));
    int paddingNew = (4 - (bi.biWidth * sizeof(RGBTRIPLE) % 4));

    //iterate each scanline
    RGBTRIPLE triple;
    for (int i = 0, l = abs(height); i < l; i++)
    {
        for (int h = 0; h < n; h++)
        {
            for (int j = 0; j < width; j++)
            {
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                fwrite(&triple, sizeof(RGBTRIPLE), n, outptr);
            }

            //skip the padding
            fseek(inptr, padding, SEEK_CUR);
            //write in the new padding
            for (int o = 0; i < paddingNew; o++)
                fputc(0x00, outptr);
        }
    }

    fclose(inptr);
    fclose(outptr);
}
0

See any problems here?

//read infile BFHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
//read outfile BIHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

SPOILER: Which file are you reading from? If you can't see it, leave a comment.

This fixes the problem in your question, but there are other issues that follow, perhaps an infinite loop. But that would be a new question. Wouldn't want to deprive you of the fun of debugging them. ;-)

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

5
  • Thanks for the response Cliff. However, I adjusted the outptr to inptr, yet still I get the error "File size limit exceeded" :-(. Btw. when I run the copy program, which has already been created by cs50 staff, I get the "unsupported format" error as well, so I though maybe I am giving it a wrong image, but idk. EDIT: I started debugging and IT IS INTENSE I WILL REPORT WHEN I AM STUCK AGAIN. – Martin Wzatek Mar 26 '16 at 16:01
  • Ok, I cant even debug properly because it quits when it gets to the fread line in the nested loop at the bottom. Is debugger buggy or am I failing at smthg again? – Martin Wzatek Mar 26 '16 at 16:07
  • Ok I give up. I just dont see it and it is killing me, trust me. I am literally banging my head into the desk for around 5 to 6 hours already and I just cant figure out this freaking task MY GOD. PLEASE HELP – Martin Wzatek Mar 26 '16 at 16:27
  • Only thing I figured out is that the biWidth equals to about 2000, which I dont get because the input file is a 8px*8px image, but it shows the same value for the biWidth variable even in the copy.c program so I assume that is not a problem. However biHeight equals to about 100 bilions so I assume that could be why the file size is exceeded? But again, in the copy.c program, it shows the same thing... I'm just fucking lost, excuse the expression – Martin Wzatek Mar 26 '16 at 16:32
  • My advice at this point is to put it aside for now. Sounds like you've been working on it too much, to the point that nothing makes sense. Sleep on it and try again tomorrow, then review the class material on this pset. I guarantee it will make more sense then. – Cliff B Mar 26 '16 at 16:59
0

So Cliff, even though you adviced me not to continue and even though I agree that was probably the best solution, I continued nevertheless because I was just going mad from not coming up with proper solution.

3 hours later I solved this madness!!!

Turned out I had to edit a lot of the code and after I got the resize thing correct and working, few problems regarding the header came up, but I dealt with those as well and now it is fully functional and has passed check50 control!

HELL YEAH! NEVER GIVE UP, YAAAAA! 8 fruiting hours spent on this but I GOT IT!!

1
  • Well, you could do that too. ;-) More importantly, YOU did it! – Cliff B Mar 26 '16 at 20:09

You must log in to answer this question.

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