【发布时间】:2017-09-23 10:31:45
【问题描述】:
我正在尝试制作一个复制程序。类似于linux中的cp函数。我可以使用 ./copy file1 file2 成功复制文件,但不知何故,来自源的权限不会复制到目标。有谁知道如何做到这一点?示例和代码如下所示:)
我的文件及其原始权限。
已成功复制文件,但未复制权限。
#define buff_s 4096
#define mod 0644
void printError(char *, char *);
main(int ac, char *txts[])
{
int input, output, n_chars;
char buf[buff_s];
struct stat file1;
struct stat file2;
stat(txts[1], &file1);
stat(txts[2], &file2);
if ( (input=open(txts[1], O_RDONLY)) == -1 )
printError("error", av[1]);
if ( (output=creat( txts[2], mod)) == -1 )
printError( "error", txts[2]);
【问题讨论】:
-
还要注意
read()和write()返回ssize_t而不是int。它们不相同。 -
感谢您指出这一点。
-
Garg365,我试试你提供的方法,我觉得也可以