目录

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

描述 (Description)

C库函数int fputs(const char *str, FILE *stream)将字符串写入指定的流,但不包括空字符。

声明 (Declaration)

以下是fputs()函数的声明。

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

参数 (Parameters)

  • str - 这是一个包含要写入的以null结尾的字符序列的数组。

  • stream - 这是指向FILE对象的指针,该对象标识要写入字符串的流。

返回值 (Return Value)

此函数返回非负值,否则在返回EOF时返回错误。

例子 (Example)

以下示例显示了fputs()函数的用法。

#include <stdio.h>
int main () {
   FILE *fp;
   fp = fopen("file.txt", "w+");
   fputs("This is c programming.", fp);
   fputs("This is a system programming language.", fp);
   fclose(fp);
   return(0);
}

让我们编译并运行上面的程序,这将创建一个文件file.txt其中包含以下内容 -

This is c programming.This is a system programming language.

现在让我们使用以下程序查看上述文件的内容 -

#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);
}

让我们编译并运行上面的程序来产生以下结果。

This is c programming.This is a system programming language.
↑回到顶部↑
WIKI教程 @2018