Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C文件复制程序不适用于目录(在linux上)_C_Linux_Copy_System Calls - Fatal编程技术网

C文件复制程序不适用于目录(在linux上)

C文件复制程序不适用于目录(在linux上),c,linux,copy,system-calls,C,Linux,Copy,System Calls,下面是一个将文件内容(第一个参数)复制到新文件(第二个参数)的程序 我正在linux上测试它,因此,例如,将用户终端的内容复制到新文件也可以: ./copy /dev/tty newFile 但是,复制当前目录的内容不起作用: ./copy . newFile 后者在打开第一个参数时不会导致错误,但不会复制任何内容。我以为目录的内容会被复制到新文件中 EDIT:这是因为linux将工作目录按标准设置为~ 复制下面的.c程序: #include <stdio.h> #include

下面是一个将文件内容(第一个参数)复制到新文件(第二个参数)的程序

我正在linux上测试它,因此,例如,将用户终端的内容复制到新文件也可以:

./copy /dev/tty newFile
但是,复制当前目录的内容不起作用:

./copy . newFile
后者在打开第一个参数时不会导致错误,但不会复制任何内容。我以为目录的内容会被复制到新文件中

EDIT:这是因为linux将工作目录按标准设置为~

复制下面的.c程序:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>


int copy(int inFileDesc,int outFileDesc);

int main (int argc, char** argv)
{
    int inputfd;
    int outputfd;
    if (argc!=3)
    {
        printf("Wrong number of arguments\n");
        exit(1);
    }
    inputfd=open(argv[1],O_RDONLY);
    if(inputfd==-1)
    {
        printf("Cannot open file\n");
        exit(1);
    }
    outputfd=creat(argv[2],0666);
    if(outputfd==-1)
    {
        printf("Cannot create file\n");
        exit(1);
    }
    copy(inputfd,outputfd);
    exit(0);
}

int copy(int inFileDesc,int outFileDesc)
{
    int count;
    char buffer[BUFSIZ];
    while((count=read(inFileDesc,buffer,sizeof(buffer)))>0)
    {
        write(outFileDesc,buffer,count);
    }
}
#包括
#包括
#包括
#包括
int副本(int inFileDesc、int OUTPILEDESC);
int main(int argc,字符**argv)
{
int-inputfd;
int输出fd;
如果(argc!=3)
{
printf(“错误的参数数量\n”);
出口(1);
}
inputfd=打开(argv[1],仅限Ordu);
如果(inputfd==-1)
{
printf(“无法打开文件\n”);
出口(1);
}
outputfd=creat(argv[2],0666);
如果(outputfd==-1)
{
printf(“无法创建文件\n”);
出口(1);
}
复制(输入FD,输出FD);
出口(0);
}
int复制(int inFileDesc,int OUTPILEDESC)
{
整数计数;
字符缓冲区[BUFSIZ];
而((count=read(infiredesc,buffer,sizeof(buffer)))>0)
{
写入(outFileDesc、缓冲区、计数);
}
}

如果您阅读
man2 open
an
man2 read

男子2公开赛

The named file is opened unless:
...
[EISDIR] The named file is a directory, and the arguments specify that it is to
be opened for writing.
男子2阅读

The pread(), read(), and readv() calls will succeed unless:
...
[EISDIR] An attempt is made to read a directory.

因此,
open
不会失败,因为您指定了
O_RDONLY
并返回了文件描述符,但是
read
会在第一次调用时失败。

首先,我看不到flock结构,如何实现O_RDONLY?O_RDONLY是在fcntl.h中定义的,我想()确实是的,但我想你需要说struct flock my_lock;和lock.l_type=O_RDONLY@korppu73在
/copy之后,您希望
newFile
包含什么内容。newFile
?@Tsakioglou:好的,谢谢,我来看看例子。我不知道弗洛克的事。谢谢所以在一个目录上读取总是失败的。但是这与上面提到的是否创建了羊群结构无关?不,你不需要在这里加锁。