使用 C 将文件从一个目录复制到另一个目录。在发送目录中打开文件时出现问题。没有这样的文件或目录

Copying files from one directory into another directory using C. Trouble with opening files in the sending directory. No such file or directory

我是系统编程的新手,在学习目录和文件的工作原理时遇到了一些困难。该程序应该有两个参数(目录),如果两个都是现有目录,则只需将文件从第一个参数复制到第二个参数。如果第二个是文件,return 报错,最后如果第二个参数不存在,则创建它并复制文件。

当我尝试打开每个文件以将内容复制到新创建的副本时出现问题。我可以获得第一个目录中所有文件的列表。如果我删除复制数据(又名。in_fd)程序复制所有文件,它们只是空文件。

到目前为止程序检查输入,如果需要创建目录。剩下的就是复制文件。

如有任何帮助,我们将不胜感激。我在其他问题上看到了这一点,但 none 的答案似乎有帮助。预先感谢您的所有帮助。

#define BUFFERSIZE      4096
#define COPYMODE        0644

void oops(char *, char *);

int main(int ac, char *av[])
{
int     in_fd, out_fd, n_chars;
char    buf[BUFFERSIZE];
/* check args   */
if ( ac != 3 ){
    fprintf( stderr, "usage: %s source destination\n", *av);
    exit(1);
}

//Directory pointers
DIR *sender_dir_ptr;
DIR *receiver_dir_ptr;

struct dirent *direntp;

//Used to test second argument for new/existing directory
struct stat info;

if(lstat(av[2],&info) != 0) {
    if(errno == ENOENT) {
        //doesn't exist, make directory
        mkdir(av[2], 0700);
        if ((receiver_dir_ptr = opendir(av[2])) == NULL )
            oops("cannot open %s\n", av[2]);

    } else if(errno == EACCES) {
        // we don't have permission to know if
        //  the path/file exists.. impossible to tell
        oops("Permission Denied", av[2]);
    }
}
//so, it exists.
if(S_ISDIR(info.st_mode)) {
    //it's a directory. Assign the directory pointer
    if ((receiver_dir_ptr = opendir(av[2])) == NULL )
        oops("cannot open %s\n", av[2]);
} else if(S_ISREG(info.st_mode)) {
    //it's a file, display error and exit
    oops("File exists but looking for a directory", av[2]);
}


if ((sender_dir_ptr = opendir(av[1])) == NULL )
    oops("cannot open %s\n", av[1]);
else
{
    struct stat st_buf;

    //Go through sender directory and copy over all files to new directory
    while (( direntp = readdir(sender_dir_ptr)) != NULL )
    {
        lstat(direntp->d_name, &st_buf);
        if (S_ISDIR (st_buf.st_mode))
        {
            continue;
        }
        else if (S_ISREG (st_buf.st_mode))
        {
            printf("direntp= %s\n",direntp->d_name);

            char tmp_in[strlen(av[1])];
            strcpy(tmp_in, av[1]);
            strcat(tmp_in, "/");
            strcat(tmp_in, direntp->d_name);

            if ((in_fd=open(tmp_in, O_RDONLY)) == -1 )
                oops("Cannot open,", direntp->d_name);

            //Create pathname to the second directory
            char* filename = av[2];
            char tmp[strlen(av[2])];
            strcpy(tmp, av[2]);
            strcat(tmp, "/");
            strcat(tmp, direntp->d_name);
            printf("filename: %s \n", tmp);

            //Create new file
            if ((out_fd=creat(tmp, COPYMODE)) == -1 )
                oops( "Cannot creat", tmp);

            //Write old file data into the new files
            while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 )
                if ( write(out_fd, buf, n_chars ) != n_chars )
                    oops("Write error to ", av[2]);
            if ( n_chars == -1 )
                oops("Read error from ", av[1]);

            //close files
            if ( close(in_fd) == -1 || close(out_fd) == -1 )
                oops("Error closing files","");
        }
        else{
            printf("File: %s \n",direntp->d_name);

        }

    }
    //Close directories
    closedir(sender_dir_ptr);
    closedir(receiver_dir_ptr);
}

return 0;
}

void oops(char *s1, char *s2)
{
fprintf(stderr,"Error: %s ", s1);
perror(s2);
exit(1);
}

'direntp->d_name'只是文件名,不是open()等所要求的完整文件规范。您需要将名称strcat到文件夹路径。