【发布时间】:2018-01-31 06:11:42
【问题描述】:
由于“权限被拒绝”,我的以下复制文件的程序不允许我复制文件。但是,我给了它权限。
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
int fdinput, fdoutput; //file pointers
char arrbuf[5000]; //size of what can be read in file
ssize_t bytesR, bytesW;//number of what input returns
mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH |S_IXOTH ;
fdinput = open(argv[1], O_RDONLY); //pointing to read file
fdoutput = open(argv[2], O_WRONLY);//pointing to write file
if(fdinput == -1){
perror("the source file cant be opened");
return 1;
}
if(fdoutput == -1){
perror("the written file cant be opened");
return 2;
}
while((bytesR = read(fdinput, arrbuf, sizeof arrbuf)) > 0){
bytesW = write(fdoutput, arrbuf, (ssize_t) bytesR);
}
close(fdinput);
close(fdoutput);
return 0;
}
【问题讨论】:
-
忘记提问了。由于权限被拒绝,我的程序不允许我复制文件,我给了它权限
-
你能补充一点解释吗?错误信息等?
-
将你的问题编辑成你的问题——不要把它变成评论。
-
您是在复制可执行程序吗?如果不是,
S_IXUSR和S_IXOTH位在权限中是不合适的(考虑到您包括其他两个,省略S_IXGRP令人费解)。请注意,您的第二个open()调用会从第一个调用中践踏错误状态(在errno中)——如果两者都失败了。如果您的open()写入文件不存在,则不会尝试创建该文件,这可能也是因为您省略了包含O_CREAT时所需的权限参数。如果您创建,请考虑O_TRUNC或O_EXCL。 -
定义和初始化
mode的目的是什么?没有使用它?
标签: c linux unix permissions system-calls