Apr 24, 2009 at 4:56am UTC
Hello guys
I did this program but as you can imagine i doesn't work properly, well indeed it doesn't work.. what i'm trying to do is to store a file into an array of structure, this is the code
struct info{
char TitleBook[20];
char Name[20];
char Year[5];
char Cost[8];
};
int main(){
char* filename;
struct info* ptr;
FILE* fptr;
int NumberBooks;
char line[20];
int i;
ptr=(struct info *)malloc(sizeof(struct info));
printf("Please enter the filename to be loaded. ");
scanf("%s",filename);
if((fptr=fopen(filename,"r"))==NULL){
printf("Cannot open the file\n");
}else{
fscanf(fptr,"%d",&NumberBooks);
for(i=0;i<NumberBooks;i++){
fgets(line,20,fptr);
strncpy(ptr[i].TitleBook,line,20);
fgets(line,20,fptr);
strncpy(ptr[i].Name,line,20);
fgets(line,20,fptr);
strncpy(ptr[i].Year,line,20);
fgets(line,20,fptr);
strncpy(ptr[i].Cost,line,20);
}
}
printf("%s has been loaded successfully\n",filename);
fclose(fptr);
free(ptr);
}
Thanks for any help.