0

我的代码编译得很好但是当我运行它时我得到总线错误:10

void backupf(char *namelist, char *dirname)
{  
char    *in_filename;
char    *out_filename;
char    line[MAXPATHLEN];

FILE    *filenames = fopen(namelist, "r");

if(filenames == NULL) 
    {
        fprintf(stderr, "Cannot Open File\n");
        exit(EXIT_FAILURE);
    }
while( fgets(line, sizeof line, filenames) != NULL )
    {
    sprintf(in_filename, "./%s\n", line);
    sprintf(out_filename, "%s/%s\n", dirname, line);
    }
    backup(dirname, in_filename, out_filename);
fclose(filenames);
}

它应该采用带有文件名列表的文本文件参数,然后使用该信息使用我编写的备份功能将其备份到备份目录。

4

1 回答 1

1

您不为文件名分配空间;你应该。你正在写不确定的记忆。这可能会更好:

void backupf(char *namelist, char *dirname)
{  
    char  in_filename[MAXPATHLEN];
    char  out_filename[MAXPATHLEN];
    char  line[MAXPATHLEN]; 
    FILE *filenames = fopen(namelist, "r");

    if (filenames == NULL) 
    {
        fprintf(stderr, "Cannot Open File\n");
        exit(EXIT_FAILURE);
    }
    while (fgets(line, sizeof(line), filenames) != NULL)
    {
        snprintf(in_filename, sizeof(in_filename)"./%s\n", line);
        snprintf(out_filename, sizeof(out_filename), "%s/%s\n", dirname, line);
        backup(dirname, in_filename, out_filename);
    }
    fclose(filenames);
}
于 2013-11-02T02:59:50.697 回答