title: fopen does not work properly and I get segmentation fault when I use fclose
body: <p>I am new c-programmer.I am writing a small student database. I have an array of structs I would like to write it to file. Till now the program works fine. I can print out the data saved in the array called db (abbreviation of database). In order to be able to write data, I opened a new c-file and I wrote a method <code>writeData()</code> that allows me to write data using <code>FILE-object</code> and <code>fopen</code>. Here are the methods of my database located in a header-file:</p>
  
  <pre><code>//header file
  #ifndef DB_OPS
  #define DB_OPS
  #define SIZE 3typedef int bool;
  typedef int bool;
  #define true 1
  #define false 0
  struct student{
      bool statusFlag;
      char lastname[20];
      char firstname[20];
      int  mNr;
      char subject[30];
      char nationality[20];
  };
  
  int createDb(int s);
  struct student getData(char * lastname, char * firstname, int matNr, char * courseOfStudy, char * nationality);
  void insert_student(struct student *  st);
  void update_student(int matNr);
  bool delete_student(int matNr);
  void display_result(bool  res, bool operation);
  bool search_student(int matNr);
  void display_db();
  void writeData();//method to write data
  void readData();
  void print();
  #endif
  </code></pre>
  
  <p>then I defined the method <code>writeData()</code>. Here is the code:</p>
  
  <pre><code>//a c-file
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include "db_ops.h"
  
  
  void writeData(){
      //when i use fopen, the data saved in db will be damaged
      FILE * fpw;
      fpw=fopen("database.txt","wb");
      if(fpw==NULL){
          printf("the file cannot be opened");
          exit(1);
      }
  
      extern struct student *db;
      int i;
      for(i=0;i&lt;SIZE;i++){
          printf("%s, %s, %d, %s , %s\n", 
              (db+i)-&gt;lastname,(db+i)-&gt;firstname,(db+i)-&gt;mNr, (db+i)-&gt;subject, (db+i)-&gt;nationality);
      }
      fclose(fpw);//When I use fclose(),I get segmentation fault: free(): invalid next size
  }
  </code></pre>
  
  <p>Till now I am not able to find a solution or an explanation for that Problem. When I use fopen, I cannot find my data saved in the array db anymore. I do know if my array db which is a pointer is pointing outside of the array. The second problem consists in using fclose, which generates a segmentation fault.Any suggestions?
  Here is a piece of code located in a file where I initialized the array db:</p>
  
  <pre><code>// another c-file
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include "db_ops.h"
  
  struct student * db;
  struct student * ptrDb;
  static int insertCounter=1;
  int size=0;
  int   createDb(int s){
      size=s;
      db= (struct student *)calloc(3,sizeof(struct student *));
      if(db==NULL){
          printf("dynamic allocation failed");
          return EXIT_FAILURE;
      }
      ptrDb=db;
      printf("database was created\n");
  }
  struct student getData(char * lastname, char * firstname, int matNr, char * subject, char * nationality){
      struct student st;
      int i;
      if(insertCounter&lt;=size){
          //get data
          st.statusFlag=1;//flag to show, whether the program gets data or not 
          //0-byte marks the end of the string
          memcpy(st.lastname,lastname,strlen(lastname)+1);
          memcpy(st.firstname,firstname,strlen(firstname)+1);
          st.mNr=matNr;
          memcpy(st.subject, subject,strlen(subject)+1);
          memcpy(st.nationality,nationality,strlen(nationality)+1);
          //printf("%s,%s,%d,%s,%s\n",st.lastname,st.firstname,st.mNr,st.subject,st.nationality);
          return st;
      }else if(insertCounter&gt;size){
          st.statusFlag=0;
          return st;
     }
  }
  //coping input by reference
  void insert_student(struct student *  st){
  
      printf("statusFlag:%d\n",st-&gt;statusFlag);
      if(st-&gt;statusFlag==1){  
          *ptrDb=*st;
          insertCounter++;
          ptrDb++;    
      }else{  
            printf("##########################################################\n");
      printf("no insert is possible, The maximum size has been reached\n");
         printf("##########################################################\n");
  }
  </code></pre>
  
  <p>}</p>