0

can anyone please tell me what the difference is beetween fread and fwrite and why do we have to fread smthg before we actually fwrite it into a file?

 // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

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

Fread reads from inptr and stores that into &triple and fwrite reads from outptr and stores that into &triple as well, right?

So I already stored the bits into &triple with fread, why do I have to do the same thing with fwrite? In this example, fwrite writes from outptr, althought that is just an empty file we have opened so what can it possibly write into &triple when there is nothing in the file?

I'm just confused, can anybody please explain?

1 Answer 1

1

from man fread:

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

The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.

from man fwrite:

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

The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.

and no, you don't have to always fread before you fwrite, but you do in this case because you wanna read the pixel form the original BMP then write it, maybe multiple times, to the resized BMP.

You must log in to answer this question.

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