我编写了一个 c 代码,旨在在根目录下创建一个文件。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
const char *path="/";
int main(){
FILE *fp;
umask(0);
chdir(path);
fp=fopen("test.txt","w+");
fclose(fp);
return 0;
}
编译没有错误,但是当我执行文件时,出现以下错误:
kwagjj@kwagjj-Inspiron-3420:~$ gcc -Wall a2.c -o a2
kwagjj@kwagjj-Inspiron-3420:~$ ./a2
Segmentation fault (core dumped)
我怎么用错了umask
函数?
最佳答案
我的钱花在 fopen()
失败(可能是由于权限不足)并返回 NULL
和 fclose(NULL)
causing the segfault 。
关于c - umask 不能在 C 代码中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25700519/