当前位置: 首页 > 图文教程 > 开发语言 > C/C++ > C/C++:小编谈C语言函数那些事(6)

C/C++
2009年编程开发语言的使用率
C++对象模型笔记:dynamic binding
cstl -- c语言编写通用数据结构和常用算法库(模仿SGI STL)
子串匹配:不回溯算法
C++ Builder 访问 USB 口的方法
C++中二维数组的动态分配
数组和指针在编译的时候的区别
如何利用doxygen生成pdf文档
有关C++析构函数的异常(Exceptions in Destructors)
C++模板学习之函数对象之谓词
5月编程语言排行榜:D语言风采不再
一个C++类实现文件全盘搜索
C语言编程宝典之一 读书笔记
C语言嵌入式系统编程修炼(内存操作)
C++内存管理
带头结点的双循环链表
有关VA_LIST的用法
C++标准库简介(转)
一个栈类的实现(链栈)
C 引用与指针的比较

C/C++:小编谈C语言函数那些事(6)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 56 ::
收藏到网摘: n/a

C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。

1.       fclose函数

fclose函数的功能是关闭一个流,其用法是:int fclose(FILE *stream); 程序例子如下:

#include <string.h>

#include <stdio.h>

int main(void)

{

   FILE *fp;

   char buf[11] = "0123456789";

   /* create a file containing 10 bytes */

   fp = fopen("DUMMY.FIL", "w");

   fwrite(&buf, strlen(buf), 1, fp);

   /* close the file */

   fclose(fp);

   return 0;

}

2.       fcloseall函数

fcloseall函数的功能是关闭打开流,其用法是:int fcloseall(void); 程序例子如下:

#include <stdio.h>

int main(void)

{

   int streams_closed;

   /* open two streams */

   fopen("DUMMY.ONE", "w");

   fopen("DUMMY.TWO", "w");

   /* close the open streams */

   streams_closed = fcloseall();

   if (streams_closed == EOF)

      /* issue an error message */

      perror("Error");

   else

      /* print result of fcloseall() function */

      printf("%d streams were closed.\n", streams_closed);

   return 0;

}

3.       fcvt函数

fcvt函数的功能是把一个浮点数转换为字符串,其用法是:char *fcvt(double value, int ndigit, int *decpt, int *sign); 程序例子如下:

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

   char *string;

   double value;

   int dec, sign;

   int ndig = 10;

   clrscr();

   value = 9.876;

   string = ecvt(value, ndig, &dec, &sign);

   printf("string = %s      dec = %d \

          sign = %d\n", string, dec, sign);

   value = -123.45;

   ndig= 15;

   string = ecvt(value,ndig,&dec,&sign);

   printf("string = %s dec = %d sign = %d\n",

          string, dec, sign);

 

   value = 0.6789e5; /* scientific

                        notation */

   ndig = 5;

   string = ecvt(value,ndig,&dec,&sign);

   printf("string = %s           dec = %d\

          sign = %d\n", string, dec, sign);

   return 0;

}

4.       fdopen函数

fdopen函数是把流与一个文件句柄相接,其用法为:FILE *fdopen(int handle, char *type); 程序代码如下:

#include <sys\stat.h>

#include <stdio.h>

#include <fcntl.h>

#include <io.h>

int main(void)

{

   int handle;

   FILE *stream;

   /* open a file */

   handle = open("DUMMY.FIL", O_CREAT,

    S_IREAD | S_IWRITE);

   /* now turn the handle into a stream */

   stream = fdopen(handle, "w");

   if (stream == NULL)

      printf("fdopen failed\n");

   else

   {

      fprintf(stream, "Hello world\n");

      fclose(stream);

   }

   return 0;

}