The C library fputc(int char, FILE *stream) function writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream. This function is part of the standard I/O library, and it is commonly used when working with file operations in C programming.
SyntaxFollowing is the C library syntax of the fputc() function −
int fputc(int char, FILE *stream);Parameters
This function accepts the following parameters −
On success, fputc returns the character written as an unsigned char cast to an int. On failure, it returns EOF (End of File) and sets the appropriate error indicator on the stream.
Example 1: Writing a Single Character to a FileThis program opens a file named "example1.txt" in write mode and writes the character 'A' to it.
Below is the illustration of C library fputc() function.
#include <stdio.h> int main() { FILE *file = fopen("example1.txt", "w"); if (file == NULL) { perror("Failed to open file"); return 1; } fputc('A', file); fclose(file); return 0; }Output
After execution of above code, the file "example1.txt" will contain the single character 'A'.
Example 2 : Writing multiple charactersThis program will create a file file.txt in the current directory which will contain the characters with ASCI values from 33 to 100.
#include <stdio.h> int main () { FILE *fp; int ch; fp = fopen("file.txt", "w+"); for( ch = 33 ; ch <= 100; ch++ ) { fputc(ch, fp); } fclose(fp); return(0); }Output
The above code produces following result−
!"#$%&'()*+,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4