I'm trying to write and read a linked list from a binary file in C. My aim is saving and loading resident data for a Nursing Home (actually, I'm a Nurse), in order to classify each resident via the Resource Utilization Groups. I've already done it for a fixed amount of residents (32, that is the capacity of the facility) using an array of structures, but now I need to do so for a variable set of residents, in order to do a statistic study. Obviously for this first attempt i simplified the structure to the minimum, as the actual structure contains 109 data.
I'm very close to the solution but something doesn't work, that is, each element of the saved list is alternated with a void one.This code should read the list from the binary file, show it on the terminal, add a new element, save the list. Of course each procedure should be put in a function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
struct pat
{
char surn [16];
char name [16];
struct pat *next;
};
static FILE *h;
static struct pat *osp;
static struct pat *first;
struct pat *make_structure (void);
int main()
{
initscr();
raw();
keypad (stdscr, TRUE);
noecho();
clear();
osp=make_structure();
first=osp;
h=fopen ("archivio","r");
if (h==NULL)
printw ("Archivio inesistenten");
else
{
while (!feof(h))
{
printw ("Lungh. nome = %dn",sizeof osp->name);
fread (osp->surn,sizeof osp->surn,1,h);
fread (osp->name,sizeof osp->name,1,h);
printw ("Cognome: %stNome: %sn",osp->surn,osp->name);
osp->next=make_structure();
osp=osp->next;
}
}
getch();
echo();
printw ("Surname: ");
scanw ("%s",osp->surn);
printw ("nName: ");
scanw ("%s",osp->name);
noecho();
osp=first;
h=fopen ("archivio","w");
while (osp != NULL)
{
fwrite (osp->surn,sizeof osp->surn,1,h);
fwrite (osp->name,sizeof osp->name,1,h);
osp=osp->next;
}
return 0;
}
struct pat *make_structure(void)
{
struct pat *a;
a = (struct pat *)malloc(sizeof(struct pat));
return (a);
}
Aucun commentaire:
Enregistrer un commentaire