Search code examples
cbinaryfilesfwritestdio

Is it possible to write a constant value to a binary file directly?


I know that

fwrite(&variable,sizeof(datatype),<count>,<filepointer>); 

writes data onto a binary file. But what if I want to write the character 'c' directly into the binary file (without using a buffer variable). I can't put,

 fwrite(&'c',sizeof(datatype),<count>,<filepointer>);

is this possible?


Solution

  • Use putc or fputc (both have the same signature):

    int putc(int c, FILE *stream);
    

    If you're interested in putc vs fputc check the comments below, they know it better than me :)