fputs()
is a standard C library function that is used to write a string of characters to a file at the location indicated by the file pointer. Below is the declaration of the fputs()
function:
int fputs (const char * str, FILE * stream);
The fputs()
function returns 0 if the string is written to the file successfully.
fputs()
functionstr
: String of characters to be writtenstream
: Pointer to the file where the character is to be writtenConsider the code snippet below, which uses fputs()
to write a string to a file:
#include <stdio.h>int main() {FILE * fPointer;const char * str = "fputs() example code";fPointer = fopen ("fputsExample.txt", "w");//opening filefputs(str, fPointer);//writing str to the filefclose(fPointer);return 0;}
The fopen()
function is used in line 5 to open the file for writing. The fputs()
function is used in line 10 to write the string str
that is created in line 6 to the file location indicated by fPointer
. After writing str
to the file successfully, fputs()
increments the fPointer
by one location so that when the next write operation is applied to the file, the writing begins after the last character of str
written by fputs()
.
The contents of fputsExample.txt
after executing the above code will be as follows:
# fputsExample.txt
fputs() example code
Free Resources