Dotcpp  >  编程教程  >  io.h头文件  >  C语言creat()函数:创建指定文件名的文件

C语言creat()函数:创建指定文件名的文件

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

函数名:creat

头文件:<io.h>

函数原型: int creat(const char *file,int auth);

功能: 创建指定文件名的文件

参数:char *file 要创建的文件名 , int auth   为操作权限

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


程序例:  创建一个文件,然后输入内容,该函数创建文件不能覆盖同名的文件

#include<stdio.h>

#include<io.h>

#include<fcntl.h>

int main(void){

   char filename[80];

   printf("input file path and file name,eg d:\\a.txt:  ");

   gets(filename);

   int fd=creat(filename,O_RDONLY);

   if(fd==-1){

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

      return 1;

   }

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

   close(fd);

   return 0;

}

 

运行结果

input file path and file name,eg d:\a.txt:  d:\c.txt
successful to create the file



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

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