A
Atulvid
Hi,
I created a test file for my application by writing structures to a
binary file. To make sure that I will get correct data for the
pointers inside the structure, i wrote actual data they pointed to and
data size/len to the file, after the structure. But when I tried to
read the file in same sequence, it gives me segmentation fault.
When I debugged the program with GDB, it executed well until
fclose() and read all the member of sturecture correrctly, but after
that at closing mains bracket it gave me SIGSEGV. I am posting code of
the read-file operation here. Please tell me why i am getting fault.
To make code compact here, I have removed checks for return values
of fread().
int main(int argc, char *argv[]){
int rc=0;
int len=0;
FILE *fp;
struct kml_rec *rec;
fp = fopen("cfile.kml","rb");
while((rc = fread(rec,sizeof(struct kml_rec),1,fp))){
rc = fread(&len,sizeof(int),1,fp); //HDR
size
rec->prefix.hdr = (struct kml_prefix_hdr
*)malloc(len);
rc = fread(rec->prefix.hdr,len,1,fp);
//Header
rc = fread(&len,sizeof(int),1,fp); //Path
Len
rec->path = (char *)malloc(len+1);
rc = fread(rec->path,len,sizeof(char),fp); //Path
rec->path[len]='\0';
rc = fread(&len,sizeof(int),1,fp); //Name
len
rec->name = (char *)malloc(len+1);
rc = fread(rec->name,len,sizeof(char),fp); //Name
rec->name[len]='\0';
rc = fread(&len,sizeof(int),1,fp);
//Target len
rec->target = (char *)malloc(len+1);
rc = fread(rec->target,len,sizeof(char),fp);
//Target
rec->target[len]='\0';
printf("%s %s %s %s\n",
commandp(rec->prefix.hdr->opcode),rec->path,rec->name,rec->target);
free(rec->prefix.hdr);
free(rec->path);
free(rec->name);
free(rec->target);
}
fflush(fp);
if((rc=fclose(fp))!=0)
printf("ERROR: in closing file.\n");
}
I created a test file for my application by writing structures to a
binary file. To make sure that I will get correct data for the
pointers inside the structure, i wrote actual data they pointed to and
data size/len to the file, after the structure. But when I tried to
read the file in same sequence, it gives me segmentation fault.
When I debugged the program with GDB, it executed well until
fclose() and read all the member of sturecture correrctly, but after
that at closing mains bracket it gave me SIGSEGV. I am posting code of
the read-file operation here. Please tell me why i am getting fault.
To make code compact here, I have removed checks for return values
of fread().
int main(int argc, char *argv[]){
int rc=0;
int len=0;
FILE *fp;
struct kml_rec *rec;
fp = fopen("cfile.kml","rb");
while((rc = fread(rec,sizeof(struct kml_rec),1,fp))){
rc = fread(&len,sizeof(int),1,fp); //HDR
size
rec->prefix.hdr = (struct kml_prefix_hdr
*)malloc(len);
rc = fread(rec->prefix.hdr,len,1,fp);
//Header
rc = fread(&len,sizeof(int),1,fp); //Path
Len
rec->path = (char *)malloc(len+1);
rc = fread(rec->path,len,sizeof(char),fp); //Path
rec->path[len]='\0';
rc = fread(&len,sizeof(int),1,fp); //Name
len
rec->name = (char *)malloc(len+1);
rc = fread(rec->name,len,sizeof(char),fp); //Name
rec->name[len]='\0';
rc = fread(&len,sizeof(int),1,fp);
//Target len
rec->target = (char *)malloc(len+1);
rc = fread(rec->target,len,sizeof(char),fp);
//Target
rec->target[len]='\0';
printf("%s %s %s %s\n",
commandp(rec->prefix.hdr->opcode),rec->path,rec->name,rec->target);
free(rec->prefix.hdr);
free(rec->path);
free(rec->name);
free(rec->target);
}
fflush(fp);
if((rc=fclose(fp))!=0)
printf("ERROR: in closing file.\n");
}