程序有什么问题?

时间:2016-11-19 10:07:34

标签: c file

程序粉碎了,请帮帮我,提前谢谢。该文件可以在下面下载。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int deed_id;
    char deed_name[55];
    int points_for_deed;
    int total_times_done;
    int total_points_earned;
} deed;

int main(){
    FILE *file1;
    file1=fopen("deed_list.txt", "r");
    if(file1==NULL){
        printf("Can not open the file");
        return 1;
    }
    int j;
    fscanf(file1, "%i", &j);
    deed  **deed_list = (deed**)malloc(sizeof(deed)*j);
    int i;
    for(i=0; i<j; i++){
        fscanf(file1, "%i %s %i", &deed_list[i]->deed_id, deed_list[i]->deed_name, &deed_list[i]->points_for_deed);
    }
    printf("%i",j);
    fclose(file1);
    return 0;
}

https://sst-csci.com/csci151/wp-content/uploads/deed_list.txt

1 个答案:

答案 0 :(得分:1)

deed  **deed_list = (deed**)malloc(sizeof(deed)*j);

您不希望指向指针的指针存储deed数组,更改为单个指针(并且不要转换malloc):

deed  *deed_list = malloc(sizeof(deed)*j);

不要忘记最后致电free(deed_list);