Latest web development tutorials

C 庫函數– fputs()

C 標準庫 - <stdio.h> C標準庫- <stdio.h>

描述

C庫函數int fputs(const char *str, FILE *stream)把字符串寫入到指定的流stream中,但不包括空字符。

聲明

下面是fputs() 函數的聲明。

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

參數

  • str --這是一個數組,包含了要寫入的以空字符終止的字符序列。
  • stream --這是指向FILE對象的指針,該FILE對象標識了要被寫入字符串的流。

返回值

該函數返回一個非負值,如果發生錯誤則返回EOF。

實例

下面的實例演示了fputs() 函數的用法。

#include <stdio.h>

int main ()
{
   FILE *fp;

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

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

   fclose(fp);
   
   return(0);
}

讓我們編譯並運行上面的程序,這將創建文件file.txt ,它的內容如下:

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

現在讓我們使用下面的程序查看上面文件的內容:

#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 標準庫 - <stdio.h> C標準庫- <stdio.h>