Segmentation fault while reading binary file in C -
im beginner in c. write , read binary file, , reached far:
i can struct binary file, , can read.
problem one: can read integers, somehow strings printed garbage or random characters.
problem two: if run program, add entries binary file, print entries working fine (except problem one), after terminate program , run again got segmentation fault while try read file.
please me, cannot move forward.
/* our structure */ struct rec { int max,cost; char *name; }; struct rec addnewentry() { //init char name[256]; int max; int cost; //input printf("type name: \n"); scanf("%s" , &name) ; printf("type guests limit: \n"); scanf("%d", &max); printf("type price: \n"); scanf("%d", &cost); //create record struct rec record; record.name = name; record.max = max; record.cost = cost; return record; } int main() { file *ptr_myfile; //////////////////////////menu//////////////////////////////// int option=-1; while(option!=3) { printf("\n=== menu === \n"); printf("\n1. print entries"); printf("\n2. add new entry"); printf("\n3. exit"); printf("\n"); printf("\ntype menu option:"); scanf("%d", &option); if(option == 1) { printf("\n...printing entries\n"); int f=open("stadionok.db",o_rdonly); if (f<0){ perror("error @ opening file\n");exit(1);} struct rec my_record; while (read(f,&my_record,sizeof(my_record))){ //use write writing printf("name: %s \n",my_record.name); printf("max: %d \n",my_record.max); printf("cost: %d \n",my_record.cost); } close(f); } else if(option ==2) { printf("\n...type new entry\n"); //open , check ptr_myfile=fopen("stadionok.db","a"); if (!ptr_myfile) { printf("unable open file!"); return 1; } //type new entry struct rec new_stad = addnewentry(); //write file fwrite(&new_stad, sizeof(struct rec), 1, ptr_myfile); //close fclose(ptr_myfile); printf("done.\n"); } } return 0; }
e d t:
i modified suggested , got: error: incompatible types in assignment
at:
char name[256]; //input printf("type name: \n"); scanf("%s" , &name) ; struct rec record; record.name = name; //here
the reason garbage strings writing char pointer file. simple way want use char array fixed length inside struct. so:
struct rec { int max,cost; char name[1024]; };
it reason crash. when read data , trying print string printf()
tries read memory block not valid, because memory addresses changed.
if want write strings dynamic size file gets bit more complicated.
should not write struct itself, values of , size of string.
pseudocode example:
write(max); write(cost); write(strlen(name)); // strlen + 1 if want include \0 write(name); max = readint(); cost = readint(); namelen = readint(); allocatememory(namelen); name = read_n_bytes(namelen);
edit: if rec.name still pointer (char*) have assign pointer it, not array.
rec.name = &name;
if rec.name array fixed size now:
strncpy(rec.name, name, sizeof(rec.name)); // copy string rec.name rec.name[strlen(name)] = '\0'; // add binary 0 end of string
note: if reading more 1 struct (in loop), should not use char name[256];
, because every rec.name pointing name overwritten everytime read new value. use following construct every entry:
char* name; //define name pointer name = malloc(256); // allocate new memory block , assign pointer name scanf("%s" , name); // no & needed because name pointer rec.name = name; // no & needed because name pointer
Comments
Post a Comment