The function int fputc(int character, FILE *stream); writes a character to the given stream and increment the internal position indicator by one.
Function prototype of fputc
- character : This is the character to be written.
- stream : A pointer to a FILE object which identifies a stream where we want to write a character.
Return value of fputc
This function returns the character written in stream, on success. If an error occurs, EOF is returned and the error indicator is set.
C program to show the use of fputc function
The following program shows the use of fputc and fgetc function to write and read characters in a stream respectively. The following program writes a string(one character at time using fputc) in a new file and again prints it's content on screen using fgetc.
#include <stdio.h> #include <string.h> int main (){ FILE *file; char *string = "fputc C Standard Library function"; int c, counter, length = strlen(string); file = fopen("textFile.txt", "w+"); for(counter = 0; counter<length; counter++){ fputc(string[counter], file); } /* Reset file pointer location to first character */ rewind(file); /* Read characters from file */ while(!feof(file)){ c = fgetc(file); printf("%c", c); } fclose(file); return(0); }
Output
fputc C Standard Library function