在C中的结构数组上使用Realloc()

时间:2017-05-26 17:58:55

标签: c memory realloc

我的结构

$("<img/>").attr("src",images[c]).load(function() {
  $('#gallery').html( this );
});

main()的

struct table_structure{
    char first_name[50];
    char last_name[50];
    char gender;
    char dob[50];
    int waist;
    int hip;
};

加载功能。在这里,我需要将我的文件加载到结构数组。 文件看起来像这样:

enter image description here 注意:我不需要占用文件的第一行。

char filename[]="C:/Users/Rustam/Desktop/data.txt";
int i;
struct table_structure *healthcare_table;
healthcare_table=(struct table_structure*)malloc(sizeof(struct table_structure))

现在的问题是我的int load_Healthcare_Table(char file_name[], struct table_structure a[]){ FILE *inFILE; inFILE=fopen(file_name,"r"); if(inFILE)printf("File opened successfuly\n"); else { printf("Failed to open the File\n"); exit(1); } fseek(inFILE,50L,SEEK_SET); int i=1; while(fscanf(inFILE,"%s%s\t%c%s%d%d",(*(a+i-1)).first_name,(*(a+i-1)).last_name,&(*(a+i-1)).gender,(*(a+i-1)).dob,&(*(a+i-1)).waist,&(*(a+i-1)).hip)!=EOF){ i++; a=(struct table_structure*)realloc(a,i*sizeof(struct table_structure)); } }; 功能仅适用于我的realloc()功能。当我读取文件的每一行时,如何动态地将内存分配给我的数组?

0 个答案:

没有答案