Easy Tutorial
❮ C Decision C Examples Read File ❯

C Library Function - fputs()

C Standard Library - <stdio.h>

Description

The C library function int fputs(const char *str, FILE *stream) writes the string to the specified stream stream, excluding the null character.

Declaration

Here is the declaration for the fputs() function.

int fputs(const char *str, FILE *stream)

Parameters

Return Value

The function returns a non-negative value; it returns EOF if an error occurs.

Example

The following example demonstrates the use of the fputs() function.

#include <stdio.h>

int main ()
{
   FILE *fp;

   fp = fopen("file.txt", "w+");

   fputs("This is C language.", fp);
   fputs("This is a system programming language.", fp);

   fclose(fp);

   return(0);
}

Let's compile and run the above program, which will create the file file.txt with the following content:

This is C language. This is a system programming language.

Now, let's use the following program to view the content of the above file:

#include <stdio.h>

int main ()
{
   FILE *fp;
   int c;

   fp = fopen("file.txt","r");
   while(1)
   {
      c = fgetc(fp);
      if( feof(fp) )
      {
          break ;
      }
      printf("%c", c);
   }
   fclose(fp);
   return(0);
}

C Standard Library - <stdio.h>

❮ C Decision C Examples Read File ❯