0

I've been working on resize.c and after a long time of hard work. I completed it but I wasn't able to understand that

Whenever I use

fwrite(&triple, sizeof(RGBTriple), atoiargv[1], outptr);

The output file becomes unsupported but whenever I use

        for(int l=0;l<atoi(argv[1]);l++)
        {


            // write RGB triple to outfile while doing horizontal resizing
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

        }

It works!!

But in the man page of fwrite() it is defined as

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

where nmemb represents the number of data elements we need to write Please enlighten me on this !!

[EDIT: code not needed to answer question, removed for Honor Code compliance.]

1 Answer 1

2

The third element, nmemb, is the number of elements to write, not the number of times to write the same element.

Say that argv[1] is 5. This code, fwrite(&triple, sizeof(RGBTriple), atoi(argv[1]), outptr);, reads the equivalent of 5 triples starting at pointer triple and writes it out to the file. It does NOT write the triple at that address 5 times.

The for loop method writes the same element the required number of times.

Side note: Instead of recalculating atoi(argv[1]) multiple times, it is better to do it once and store it in a var.

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

1
  • Thanks a lot Really now its very clear to me !! Commented Jan 26, 2018 at 0:50

You must log in to answer this question.

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