Latest web development tutorials

C library functions - fputs ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsint fputs (const char * str, FILE * stream) string is written to the specified stream in the stream, but does not include a null character.

statement

Here is () statement fputs function.

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

parameter

  • str - This is an array that contains the sequence of characters to be written to the null terminated.
  • stream - This is a pointer to FILE pointer to an object, the object identifier FILE is to be written to a string stream.

return value

This function returns a non-negative value if an error occurs returns EOF.

Examples

The following example demonstrates fputs () function is used.

#include <stdio.h>

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, which will create a filefile.txt, which reads as follows:

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

Now let's use the following procedure to view the contents 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 standard library - <stdio.h>