fopen does not work properly and I get segmentation fault when I use fclose

198 views Asked by At

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 writeData() that allows me to write data using FILE-object and fopen. Here are the methods of my database located in a header-file:

//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

then I defined the method writeData(). Here is the code:

//a c-file
#include <stdlib.h>
#include <stdio.h>
#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<SIZE;i++){
        printf("%s, %s, %d, %s , %s\n", 
            (db+i)->lastname,(db+i)->firstname,(db+i)->mNr, (db+i)->subject, (db+i)->nationality);
    }
    fclose(fpw);//When I use fclose(),I get segmentation fault: free(): invalid next size
}

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:

// another c-file
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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<=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>size){
        st.statusFlag=0;
        return st;
   }
}
//coping input by reference
void insert_student(struct student *  st){

    printf("statusFlag:%d\n",st->statusFlag);
    if(st->statusFlag==1){  
        *ptrDb=*st;
        insertCounter++;
        ptrDb++;    
    }else{  
          printf("##########################################################\n");
    printf("no insert is possible, The maximum size has been reached\n");
       printf("##########################################################\n");
}

}

2

There are 2 answers

0
i486 On BEST ANSWER

Why calloc(3,sizeof(struct student *)) is for struct pointer but not struct member? It seems has to be calloc(3,sizeof(struct student)) for 3 structure members.

3
i486 On

It is not known whether student struct pointer and contents are correct. Try to fclose the file immediately after fopen. If there is no segmentation fault, then your problem is in pointer or memory allocation.