Segmentation fault with file io while closing main

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");
}
 
J

Jirka Klaue

Atulvid said:
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))){

Where does rec point? Nowhere!

So either write:
struct kml_rec *rec = malloc(sizeof *rec);
or:
struct kml_rec rec = {0};
fread(&rec, ...

Jirka
 
M

Martin Ambuhl

Atulvid said:
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)))

You never allocated space for rec to point to. This is easy to fix with
using malloc:
struct kml_rec rec; /* no longer a pointer */
^ notice no '*'
while((rc = fread(&rec,sizeof rec,1,fp)))
^ notice '&', and delinking the size from the
type
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top