fputs C Library Function

The function int fputs(const char *str, FILE *stream); writes a null terminated string pointed by str to the given stream. This terminating null-character(‘\0’) is not copied to the stream.

Function prototype of fputs

int fputs(cons char *str, FILE *stream);
  • str : This is the pointer to a null terminated string to be written to stream.
  • stream : A pointer to a FILE object which identifies a stream where we want to write a string.

Return value of fputs

This function returns a non-negative value on success. If an error occurs, EOF is returned and the error indicator is set.

C program using fputs function

The following program shows the use of fputs function to write a string to stream respectively.

fputs C Library Function

#include <stdio.h>
#include <string.h>
 
int main (){
   FILE *file;
   char *string = "fputs C Standard Library function";
   int c;
 
   file = fopen("textFile.txt", "w+");
   /* Writing string to a file(stream) */
   fputs(string, 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

fputs C Standard Library function