当我使用fclose

时间:2015-06-25 16:54:36

标签: c segmentation-fault fopen fclose

我是新的程序员。我正在写一个小型学生数据库。我有一个结构数组,我想把它写入文件。直到现在该程序工作正常。我可以打印出数组中保存的数据db(数据库的缩写)。为了能够写入数据,我打开了一个新的c文件,并编写了一个方法writeData(),允许我使用FILE-objectfopen来编写数据。以下是我的数据库位于头文件中的方法:

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

然后我定义了方法writeData()。这是代码:

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

直到现在我无法找到该问题的解决方案或解释。当我使用fopen时,我无法再找到保存在数组db中的数据。我知道我的数组db是一个指针指向数组之外。第二个问题在于使用fclose,它会产生分段错误。有什么建议吗? 这是一段代码,位于我初始化数组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 个答案:

答案 0 :(得分:0)

不知道学生结构指针和内容是否正确。尝试在fopen后立即关闭文件。如果没有分段错误,那么问题在于指针或内存分配。

答案 1 :(得分:0)

为什么calloc(3,sizeof(struct student *))是结构指针而不是结构成员?对于3个结构成员来说似乎必须是calloc(3,sizeof(struct student))