【问题标题】:Copying files from one directory into another directory using C. Trouble with opening files in the sending directory. No such file or directory使用 C 将文件从一个目录复制到另一个目录。在发送目录中打开文件时遇到问题。没有相应的文件和目录
【发布时间】:2015-02-02 12:42:40
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 不需要exit(1); 主要。请改写return 1;
  • lstat 不需要完整路径,而不仅仅是文件名? open 也一样?
  • @ScottHunter - 确实如此。
  • 如何获得完整路径。如果仅将目录名称作为参数给出?

标签: c file-io system


【解决方案1】:

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

【讨论】:

  • 感谢您的回答。我会投票给你,但除非我的声望达到 15 或更高,否则它不会让我投票。
猜你喜欢
  • 2020-06-22
  • 2017-11-18
  • 2012-02-15
  • 2021-02-15
  • 2013-06-01
  • 2014-08-12
  • 2013-10-24
  • 2011-10-24
相关资源
最近更新 更多