Dotcpp  >  编程教程  >  io.h头文件  >  C语言close()函数:用于关闭由open()函数所打开的文件

C语言close()函数:用于关闭由open()函数所打开的文件

点击打开在线编译器,边学边练

函数名:close

头文件:<io.h>

函数原型: int close(int handle);

功能: 用于关闭由open()函数所打开的文件

参数:int handle  打开文件时所返回的文件句柄 

返回值:成功  返回0 ,失败  返回-1


程序例:将open函数打开的文件关闭,并输出提示

#include<stdio.h>

#include<io.h>

#include<fcntl.h>

int main(void){

   int fd=open("D:\\a.txt",O_RDONLY+O_CREAT);

if(fd==-1){

      printf("can not open the file\n");

      return 1;

   }

   char *str=(close(fd)==0)?"successful close":"fail close";

   printf("%s\n",str);

   return 0;

}

 

运行结果:

successful close



本文固定URL:https://www.dotcpp.com/course/452

C语言函数库
assert.h头文件
ctype.h头文件
float.h头文件
io.h头文件
math.h头文件
mem.h头文件
setjmp.h头文件
stdio.h头文件
stdlib.h头文件
sigal.h头文件
string.h头文件
time.h头文件
Dotcpp在线编译      (登录可减少运行等待时间)