Segmentation fault with file io while closing main

Discussion in 'C Programming' started by Atulvid, Aug 11, 2003.

  1. Atulvid

    Atulvid Guest

    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");
    }
     
    Atulvid, Aug 11, 2003
    #1
    1. Advertising

  2. Atulvid

    Jirka Klaue Guest

    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
     
    Jirka Klaue, Aug 11, 2003
    #2
    1. Advertising

  3. 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
     
    Martin Ambuhl, Aug 11, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mufasa
    Replies:
    1
    Views:
    644
    Maxim Yegorushkin
    Jul 21, 2005
  2. H.S.
    Replies:
    9
    Views:
    768
    Richard Herring
    Aug 13, 2008
  3. sanket
    Replies:
    11
    Views:
    1,946
    sanket
    Apr 15, 2009
  4. Zev Blut
    Replies:
    4
    Views:
    161
    Zev Blut
    May 25, 2004
  5. Replies:
    3
    Views:
    145
    Xavier
    Apr 27, 2006
Loading...

Share This Page