fopen 无法正常工作,并且在使用 fclose 时出现分段错误

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

我是新来的 c-programmer.I 正在编写一个小型学生数据库。我有一个结构数组,我想将它写入文件。到目前为止,该程序运行良好。我可以打印出保存在名为 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,它会生成分段 fault.Any 建议? 这是一段代码,位于我初始化数组 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");
}

}

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

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