C programming stdio.h function - int fputs(const char *str, FILE *stream)

The fputs function in the stdio.h library in C programming language is used to write a string to the specified file. It writes the string pointed to by str to the output stream specified by stream.

The function has the following syntax:

r‮i:ot refe‬giftidea.com
int fputs(const char *str, FILE *stream);

Here, str is a pointer to a string that will be written to the specified file stream. stream is a pointer to a FILE object that identifies the stream.

The function returns a non-negative value on success and EOF on error. If an error occurs, the file position indicator may be set to an indeterminate position.

Example usage:

#include <stdio.h>

int main() {
   char str[] = "This is a sample string.";
   FILE *fp;

   fp = fopen("output.txt", "w");
   if (fp == NULL) {
      perror("Error opening file");
      return -1;
   }

   if (fputs(str, fp) == EOF) {
      perror("Error writing to file");
      return -1;
   }

   fclose(fp);
   return 0;
}

This code opens a file called output.txt in write mode and writes the string "This is a sample string." to it using the fputs function. If an error occurs, the perror function is used to print a message describing the error. Finally, the file is closed using the fclose function.