0

I am having some trouble in the following code :

void bufferwrite(RGBTRIPLE rgb, RGBTRIPLE* buffer, int n, int padding)
{
    fwrite(&rgb, sizeof(RGBTRIPLE), n, buffer);
    for(int i = 0; i < padding; i++)
    {
        fputc(0x00, buffer); 
    }
}

And the following error :

Error: incompatible pointer types passing 'RGBTRIPLE *' to parameter of type 
'FILE *' (aka 'struct _IO_FILE *')

[-Werror,-Wincompatible-pointer-types]

    fwrite(&rgb, sizeof(RGBTRIPLE), n, buffer);
                                       ^~~~~~
/usr/include/stdio.h:716:38: note: passing argument to parameter '__s' here
                  size_t __n, FILE *__restrict __s);
                                               ^

resize.c:151:21: error: incompatible pointer types passing 'RGBTRIPLE *' 
to parameter of type 'FILE *' (aka 'struct _IO_FILE *')
      [-Werror,-Wincompatible-pointer-types]

    fputc(0x00, buffer); 
                ^~~~~~

/usr/include/stdio.h:573:34: note: passing argument to parameter '__stream' here
extern 

    int fputc (int __c, FILE *__stream);
                             ^

So I understand the error here that I am passing a RGBTRIPLE pointer as argument instead of a FILE* pointer.

So my question here is that if I want to write to a RGBTRIPLE buffer array, then what should I do.

1
  • I am using the approach of writing all the pixels of a row of small file according to the resize factor first in a buffer and then the whole buffer in the resized file to reduce the number of writes in the final resized file. Please give your thoughts on this approach as well. Thanks
    – kunal
    Dec 10 '15 at 19:22
1

i think you are over thinking on how to write RGB array.From the videos and bmp code you will notice that RGBTRIPLE is a type(just like int and char).So when you want to initialize an array of type int,what do you do? Answer:int my_array[5]; Where 'int' is the type,'my_array'is the name of the type,'5' is the number of array you want, and '[]' indicates its an array.so you do the same for array of type RGBTRIPLE.Hope its clear.

0

EDIT

I didn't understand your doubt yet. Did you have issues in understanding the language?

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.

thus said so beautifully.

TIPS
  • Read the man pages again

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

has 4 arguments and it returns some data of type size_t.

Argument#1 const void *ptr Clearly this is not the same as a FILE pointer. So it must be the source of the data or appropriately the address of the data to be written into some location, here, a file pointed to by stream and not ptr, as you have understood.

Argument#4 FILE *stream is clearly a file pointer which is the destination of the data.

Yeah that was on purpose, I gave you instructions for opening the manual pages for fread(....) hoping that you would look for fwrite(....) in the link, or in your terminal, but it seems you still had doubts.

Previous version:

Please take a look at the manpage for fread you may want to see it online . If you read it carefully, you will understand that the function fread takes 4 parameters, two of type size_t and two pointers.

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

Now read the description. It clearly says, 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.

Now ptr must be the pointer to the memory location where you intend to store the value of the RGBTRIPLE, which shouldn't be hard for you

I believe that is the answer to your question.

2
  • Hi, my question is that how should I write in a buffer of RGBTRIPLE array because I understand from the man pages of fwrite that I am passing a ptr of RGBTRIPLE type rather than FILE*. And what I also fail to understand from your comment is that I have not mentioned fread anywhere in my question but u have mentioned as a hint. Could you care to explain that as well. Thanks
    – kunal
    Dec 12 '15 at 4:30
  • Take a seemingly straight answer, Try making void bufferwrite(RGBTRIPLE rgb, RGBTRIPLE* buffer, int n, int padding) to void bufferwrite(RGBTRIPLE rgb, FILE* buffer, int n, int padding) If you are really trying to copy data from a memory location to a file using function bufferwrite
    – vharsh
    Dec 17 '15 at 10:39

You must log in to answer this question.

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