Dotcpp  >  编程教程  >  io.h头文件  >  C语言open()函数:打开一个文件

C语言open()函数:打开一个文件

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

函数名:open

头文件:<io.h>

函数原型: int open(char *path,int access[,int auth]);

功能: 打开一个文件

参数:char *path 要打开的包含路径的文件名 ,int access  为打开方式 , int auth   为访问权限

返回值: 成功  返回文件句柄 ,失败  返回-1


程序例:打开一个文件,并输出提示

#include<stdio.h>

#include<io.h>

#include<fcntl.h>

int main(void){

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

   if(fd==-1){

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

      return 1;

   }

   printf("successful to open the file\n");

   close(fd);

   return 0;

}

 

运行结果

successful to open the file



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

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在线编译      (登录可减少运行等待时间)