writing structure to a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prashant.khade1623@gmail.com

    writing structure to a file

    Hi

    I am trying ti write a structure to a file, but I am getting
    segmentation fault. Below is my program

    #include<stdio. h>
    main()
    {
    FILE *fp;
    struct student {
    int age;
    char name[19];
    char school[15];
    };

    struct student s1={25,"khade", "KVOFPM"};
    struct student s2={25,"khade", "KVOFPM"};

    fp = fopen("khade"," r+");

    fwrite(&s1,size of(struct student ),1,fp);
    fwrite(&s2,size of(struct student ),1,fp);


    }


    Can you tell me why I am getting the error.
    Also I want to read from the file. how can I do that ?

    Thanks in advance
  • Richard Heathfield

    #2
    Re: writing structure to a file

    prashant.khade1 623@gmail.com said:
    Hi
    >
    I am trying ti write a structure to a file, but I am getting
    segmentation fault. Below is my program
    >
    <snip>
    fp = fopen("khade"," r+");
    if(fp == NULL)
    {
    printf("Aha! THAT's why.\n");

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • prashant.khade1623@gmail.com

      #3
      Re: writing structure to a file

      On Apr 12, 2:37 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
      prashant.khade1 ...@gmail.com said:
      >
      Hi
      >
      I am trying ti write a structure to  a file, but I am getting
      segmentation fault. Below is my program
      >
      <snip>
      >
      fp = fopen("khade"," r+");
      >
      if(fp == NULL)
      {
        printf("Aha! THAT's why.\n");
      >
      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999
      I am not able to open the file. What can be the reason for that ?

      Comment

      • Malcolm McLean

        #4
        Re: writing structure to a file


        <prashant.khade 1623@gmail.comw rote in message
        news:39155f01-d7bc-4067-90bb-f46d600c028f@n1 g2000prb.google groups.com...
        Hi
        >
        I am trying ti write a structure to a file, but I am getting
        segmentation fault. Below is my program
        >
        #include<stdio. h>
        main()
        {
        FILE *fp;
        struct student {
        int age;
        char name[19];
        char school[15];
        };
        >
        struct student s1={25,"khade", "KVOFPM"};
        struct student s2={25,"khade", "KVOFPM"};
        >
        fp = fopen("khade"," r+");
        >
        fwrite(&s1,size of(struct student ),1,fp);
        fwrite(&s2,size of(struct student ),1,fp);
        >
        >
        }
        >
        >
        Can you tell me why I am getting the error.
        Also I want to read from the file. how can I do that ?
        >
        Check fp(). It is probably null. Why are you using "r+" instead of "w" ?
        Otherwise your code is correct, but fclose() afterwards and, for Brownie
        points, check for write errors.
        fopen with "r" and call fread() to get an identical structure back in.
        Also strictly you should be using binary mode, because you are dumping the
        structures in binary images, not as text.

        --
        Free games and programming goodies.


        Comment

        • Pradeep

          #5
          Re: writing structure to a file

          Hi
          In your program
          file is opening in 'r+' mode
          'r+' opens the file for both reading and writing.
          but it won't check whether file is there or not.
          (i.e., it won't create new file)
          in that case you need to create the file in 'w' mode
          that will solve your problem.
          or if you don't want to change your code, then you need to create a
          blank file in the current directory with the same name you are opening
          the file in 'r+' mode(crete a file blank file "khade" in the current
          directory) then your code should work.


          On Apr 12, 2:20 pm, "prashant.khade 1...@gmail.com"
          <prashant.khade 1...@gmail.comw rote:
          Hi
          >
          I am trying ti write a structure to a file, but I am getting
          segmentation fault. Below is my program
          >
          #include<stdio. h>
          main()
          {
          FILE *fp;
          struct student {
          int age;
          char name[19];
          char school[15];
          >
          };
          >
          struct student s1={25,"khade", "KVOFPM"};
          struct student s2={25,"khade", "KVOFPM"};
          >
          fp = fopen("khade"," r+");
          >
          fwrite(&s1,size of(struct student ),1,fp);
          fwrite(&s2,size of(struct student ),1,fp);
          >
          }
          >
          Can you tell me why I am getting the error.
          Also I want to read from the file. how can I do that ?
          >
          Thanks in advance

          Comment

          Working...