0

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");
}

}

amitakCs
  • 355
  • 9
  • 25
  • 3
    Since we have no idea where you get / create your `db` pointers it's not possible for us to ascertain what you've done right or wrong. Please create the smallest possible code to fully display what you're doing. – KevinDTimm Jun 25 '15 at 16:58
  • from the man page for fopen `w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.` So, to leave your existing contents alone, use `w+`. To write `binary` data, use `wb+` http://man7.org/linux/man-pages/man3/fopen.3.html – KevinDTimm Jun 25 '15 at 17:02

2 Answers2

0

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.

i486
  • 6,491
  • 4
  • 24
  • 41
  • So look at the code where student is initialized and worked with before the call to writeData()... – Dronz Jun 25 '15 at 17:05
  • @amitakCs - well, if you have already done some debugging, why did you not tell us about it? – Martin James Jun 25 '15 at 17:09
  • Well, The data was entered correctly. I can print out the data. I can make insertion, deletion. I had no problem when working with data. The only problem that I found is the following: When I only use fopen, I cannot find my data. But When i comment out fopen, I have no problem anymore. So now i do not have any idea. If the problem persists, I will use gdb to debug the code – amitakCs Jun 25 '15 at 17:19
0

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.

i486
  • 6,491
  • 4
  • 24
  • 41