0

Here is what I've got for PSET4: Resize so far. I get a segfault with proper usage of the program (i.e ./resize n infile outfile), and I have no clue where it's coming from. Furthermore, is my approach to horizontal resizing correct? When writing the RGBTRIPLE "triple" to the new scanline, I simply write the triple "n" times (I used "scale" as the variable for "n") by passing in "scale" as the in the fwrite() function.

// Copies a BMP file

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: ./resize n infile outfile\n");
        return 1;
    }

    // remember filenames
    char *infile = argv[1];
    char *outfile = argv[2];

    // if second element in argv[] array is not a number, return 1 to main()
    if (!isdigit(argv[1]))
    {
        return 1;
    }

    int scale = atoi(argv[1]);

[EDIT: unrelated code removed for Honor Code compliance.]

1 Answer 1

1

There are a couple of issues.

First, the arg numbers used for the filenames are incorrect. I'll let you figure out why.

Second, the seg fault, is being caused here:

    if (!isdigit(argv[1]))

argv[1] is a string. isdigit takes a single char. The code is trying to shove a string down isdigit's throat and it chokes, generating the seg fault. It's the wrong approach anyways. This would only check that one char is a digit, not the whole string.

Better to convert the string to an int, as you did later when you assigned the value to scale, and test that it is within the required range from the program specification.

As for whether this is the correct approach, well, why don't you test it after making the fixes. ;-)

When you move from scale=1 to scale=2, you'll also have to make some corrections in the headers. Happy coding! ;-)

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

6
  • Thank you sooooo much. I will test this and comment when I try to continue after fixing! Commented Aug 14, 2018 at 1:37
  • The segfault is fixed (yay), but the output image is wonky.. I don't quite see where I'd have to make corrections for the headers. Could you please nudge me in the right direction in regards to that issue? :) thanks again Commented Aug 14, 2018 at 2:19
  • Well, you need to check the calculations that are showing up as wrong. It's pretty straightforward. If you have specific questions, please open a new question. You should be testing with a scale factor of 2. You are using the peek tool to check the headers, right?
    – Cliff B
    Commented Aug 14, 2018 at 10:19
  • Yeah, using the peek tool shows that no matter what scale factor I use, the headers end up being exactly the same. I'll check it out and post a question if I can't understand the reasoning behind this Commented Aug 15, 2018 at 0:33
  • @Cliff B I was doing exactly the same as Sammy with "if (!isdigit(argv[1]))" I thought that as long as you enter say '4' as argv[1] then isdigit would do it's job as per the example in the CS50 reference. I'm a bit confused. When you say that isdigit chokes on the string is it because a sting includes the '/0' ?
    – James
    Commented Jan 18, 2019 at 8:37

You must log in to answer this question.

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