Syntax
#include <stdio.h> int fputc(int c, FILE *stream);Description
fputc converts c to an unsigned char and then writes c to the output stream at the current position and advances the file position appropriately. If the stream is opened with one of the append modes, the character is appended to the end of the stream.
fputc is identical to putc is always implemented as a function call; it is never replaced by a macro.
fputc returns the byte written. A return value of EOF indicates an error.
This example writes the contents of buffer to a file called myfile.dat.
Note: Because the output occurs as a side effect within the second expression of the for statement, the statement body is null.
#include <stdio.h> #include <stdlib.h> #define NUM_ALPHA 26 int main(void) { FILE *stream; int i; int ch; /* Don't forget the NULL char at the end of the string! */ char buffer[NUM_ALPHA+1] = "abcdefghijklmnopqrstuvwxyz"; if ((stream = fopen("myfile.dat", "w")) != NULL) { /* Put buffer into file */ for (i = 0; (i < sizeof(buffer)) && ((ch = fputc(buffer[i], stream)) != EOF); ++i) ; fclose(stream); return 0; } else perror("Error opening myfile.dat"); return EXIT_FAILURE; /**************************************************************************** The output data file myfile.dat should contain: abcdefghijklmnopqrstuvwxyz ****************************************************************************/ }Related Information