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

C/C++
C和C++的特点
pragma 预处理指令详解
C++ 中什么是内联函数
C/C++没有数组
C/C++返回内部静态成员的陷阱
学好C/C++的办法
C/C++中时间函数的介绍
c/c++混合编程
c/C++内存分配
[转]浅谈C语言学习与C++语言学习的关系
托管C++
windows进程中的内存结构
C++学习重点分析
浅析scanf()函数中%[]格式控制符
C/C++:一个跨平台的 C++ 内存泄漏检测器
C/C++:C/C++时间函数使用方法
C/C++:线程冲突你了解多少?
C/C++:小编浅谈函数宏应用优缺点
C/C++:小编谈C语言函数那些事(1)
C/C++:小编谈C语言函数那些事(2)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 57 ::
收藏到网摘: 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;

}