gpt4 book ai didi

c - fopen 无法正常工作,当我使用 fclose 时出现段错误

转载 作者:行者123 更新时间:2023-11-30 17:13:05 26 4
gpt4 key购买 nike

我是新的 C 程序员。我正在编写一个小型学生数据库。我有一个结构数组,我想将其写入文件。到目前为止,该程序运行良好。我可以打印出保存在名为 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 (一个指针)是否指向数组之外。第二个问题在于使用 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");
}

}

最佳答案

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

关于c - fopen 无法正常工作,当我使用 fclose 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31056322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com