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

时间:2022-04-11 15:04:13

I am brand new to System Programming and am having some trouble learning how directories and files work. The program should take two arguments(directories), if both are existing directories then just copy the files from the first argument into the second. If the second is a file, return with an error, finally if the second argument doesn't exist, then create it and copy the files over.

我是系统编程的新手,在学习目录和文件的工作方式时遇到了一些麻烦。程序应该采用两个参数(目录),如果两个都是现有目录,那么只需将文件从第一个参数复制到第二个参数。如果第二个是文件,则返回错误,最后如果第二个参数不存在,则创建它并复制文件。

The problem occurs when I am trying to open each file to copy over the contents to the new created copy. I can get the list of all files in the first directory. If I remove copy the data(aka. in_fd) the program copies all the files over, they just are empty files.

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

So far the program checks the input, makes the directory if needed. All that is left is to copy over the files.

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

Any help will be appreciated. I saw this on other questions but none of the answers seemed to help. Thank you in advance for all your help.

任何帮助将不胜感激。我在其他问题上看到了这一点,但没有一个答案似乎有所帮助。提前感谢您的帮助。

#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);
}

1 个解决方案

#1


2  

'direntp->d_name' is filename only, not a complete file specification as required by open() etc. You need to strcat the name to the folder path.

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

#1


2  

'direntp->d_name' is filename only, not a complete file specification as required by open() etc. You need to strcat the name to the folder path.

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