By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

Segmentation fault with file io while closing main

P: n/a
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");
}
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Atulvid wrote:
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

Nov 13 '05 #2

P: n/a
Atulvid wrote:
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

--
Martin Ambuhl

Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.