总线错误:C中为10

时间:2013-11-02 02:46:13

标签: c backup bus-error

我的代码编译得很好但是当我运行它时我得到总线错误: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);
}

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

1 个答案:

答案 0 :(得分: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);
}