0

I've been working on this pset for quite a few days now, but my program doesn't seem to be compiling. It keeps saying "could not create outfile"(my message). I have no idea what to do. Any help would be appreciated.

Btw, I got this error when I tried running ./resize 4 small.bmp large.bmp.

include

include

include

include "bmp.h"

int main(int argc, char *argv[]) { //Checks that three arguments are entered if (argc != 4) { printf("Enter three arguments\n"); return 1; }

//Checks that the first argument is an integer less than or equal to 100
if (atoi(argv[2]) >= 100)
{
    printf("Integer must be less than or equal to 100\n");
    return 2;
}

int n = atoi(argv[2]);

// Creates pointer for the file
char *infile = argv[3];
char *outfile = argv[4];


// Opens infile and stores it's address in inptr
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("could not open infile, %s!\n", infile);
    return 3;
}

//Opens outfile
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
    fclose(inptr);
    printf("could not create outfile, %s!\n", outfile);
    return 4;
}

// Read infoheader into inptr file
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
2

As you know arrays start with index 0. it means name of outfile should be char *outfile = argv[3]; , not argv[4].

  • Thank you so much. That worked perfectly. I feel so stupid. That was such a simple mistake. – Kevin Oct 30 '19 at 6:14
  • If that answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-) – Christopher Holder Nov 5 '19 at 10:40

You must log in to answer this question.

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