【发布时间】:2010-09-23 10:16:28
【问题描述】:
我创建了一个文件夹,并在我打开该文件夹内的文件后对其进行写入。 碰巧在那之后我尝试打开文件但我没有权限因此我必须手动更改它。
/* 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;
}
为了获得打开创建的文件的权限,我应该更改什么?
非常感谢,
:s
with = 0_CREATE 我仍然有没有权限读取文件的问题。 我必须手动设置它们
我已经有 0_CREAT 了
打开(str1,O_RDWR | O_CREAT | O_TRUNC);
【问题讨论】:
-
您应该检查 mkdir() 系统调用的返回状态;它可能会失败。目前尚不清楚为什么 filefd 会是负面的,但这是代码片段问题的一部分,而不是完整(非)工作示例。
-
您还应该检查 open() 系统调用的返回状态;它可能会失败。我还在 open() 调用后发现 memcpy() 操作难以理解。
标签: c file-permissions