Dotcpp  >  编程教程  >  io.h头文件  >  C语言chmod()函数:用于改变文件访问方式

C语言chmod()函数:用于改变文件访问方式

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

函数名:chmod

头文件:<io.h>

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

功能:用于改变文件访问方式

参数:const char *file   为要修改的文件名 , int auth  为要修改的权限,其值为S_IREAD,S_IWRITE或S_IEXEC 

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


程序例:将指定的文件修改为只读模式

#include<stdio.h>

#include<io.h>

#include<sys/stat.h>

void make_read_only(char *filename);

int main(void){

   make_read_only("D:\\a.txt");

   make_read_only("MYFILE.FIL");

   return 0;

}

void make_read_only(char *filename){

   int stat = chmod(filename, S_IREAD);

   if (stat)

      printf("Couldn't make %s read-only\n", filename);

   else

      printf("Made %s read-only\n", filename);

}

 

运行结果:

Couldn't make D:\a.txt read-only
Couldn't make MYFILE.FIL read-only



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

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