I have a struct:
typedef struct student
{
char fname[30];
char sname[30];
char tname[30];
Faculty fac;
int course;
char group[10];
int room;
int bad;
} Student;
I read it from the file:
Database * dbOpen(char *fname)
{
FILE *fp = fopen(fname, "rb");
List *lst, *temp;
Student *std;
Database *db = malloc(sizeof(*db));
if (!fp)
return NULL;
FileNameS = fname;
std = malloc(sizeof(*std));
if (!fread(std, sizeof(*std), 1, fp)) {
db->head = db->tail = NULL;
return db;
}
lst = malloc(sizeof(*lst));
lst->s = std;
lst->prev = NULL;
db->head = lst;
while (!feof(fp)) {
fread(std, sizeof(*std), 1, fp);
temp = malloc(sizeof(*temp));
temp->s = std;
temp->prev = lst;
lst->next = temp;
lst = temp;
}
lst->next = NULL;
db->tail = lst;
fclose(fp);
return db;
}
And I have a problem... At the last record i have a such file pointer:
`fp 0x10311448 {_ptr=0x00344b90 "???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? _ _iobuf *
`
And i read last record 2 times...
Save file code:
void * dbClose(Database *db)
{
FILE *fp = fopen(FileNameS, "w+b");
List *lst, *temp;
lst = db->head;
while(lst != NULL) {
fwrite(lst->s, sizeof(*(lst->s)), 1, fp);
temp = lst;
lst = lst->next;
free(temp);
}
free(db);
fclose(fp);
}