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>