我已经创建了一个文件夹,在我打开该文件夹内的文件之后就写了一个文件.碰巧之后,我尝试打开文件,但我没有权限,因此我必须手动更改它.
/* str1 has tha name of the folder */
/* str the bytes I want to write in the file inside the folder*/
...
mkdir(str1,0777);
if (filefd < 0) {
strncpy(auxstr, str, MAX_MSG + 1);
strcat(str1,"\\");
strcat(str1, auxstr);
filefd = open (str1, O_RDWR | O_CREAT | O_TRUNC);
nbytes -= (strlen(str) + 1);
memcpy(auxstr, &str[strlen(str)+1], nbytes);
memcpy(str, auxstr, nbytes);
}
/*write to the file */
if ((nwritten = write(filefd, str, nbytes)) != nbytes) {
printf ("can't write on file\n");
break;
}
Run Code Online (Sandbox Code Playgroud)
为了拥有打开创建文件的权限,我应该更改什么?
非常感谢,
:■
with = 0_CREATE I STILL存在无权读取文件的问题.我必须手动设置它们
我已经开放了0_CREAT
open(str1,O_RDWR | O_CREAT | O_TRUNC);
你忘记了第三个参数open().
open()with 的第三个参数O_CREAT恰恰是新创建的文件具有的权限.
参考文献: