Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - fputs()

C Library Function - fputs()

C Standard Library - <stdio.h> C Standard Library -

Description

C Library Function **int fputs(const char str, FILE stream) Write the string to the specified stream stream, without the null character.

Declaration

Below is the declaration of the fputs() function.



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

Parameters

Return Value

This function returns a nonnegative value, or EOF if an error occurs.

Example

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



#include 



int main ()

{

   FILE *fp;



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



   fputs("这是 C 语言。", fp);

   fputs("这是一种系统程序设计语言。", fp);



   fclose(fp);

   

   return(0);

}

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



这是 C 语言。这是一种系统程序设计语言。

Now let us use the following program to view the content of the file above:



#include 



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 Standard Library -