我正在尝试从C中的二进制文件中编写和读取链接列表.我的目的是为护理之家保存和加载常驻数据(实际上,我是护士),以便通过资源利用率对每个居民进行分类组.我已经使用一系列结构为固定数量的居民(32,即设施的容量)做了这件事,但现在我需要为一组可变的居民做这件事,以便进行统计研究.显然,对于第一次尝试,我将结构简化为最小,因为实际结构包含109个数据.
我非常接近解决方案,但有些东西不起作用,也就是说,保存列表的每个元素都与一个空格一起交替.这个代码应该读取二进制文件中的列表,在终端上显示它,添加一个新元素,保存列表.当然,每个程序都应该放在一个函数中.
#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 inesistente\n");
else
{
while (!feof(h))
{
printw ("Lungh. nome = %d\n",sizeof osp->name);
fread (osp->surn,sizeof osp->surn,1,h);
fread (osp->name,sizeof osp->name,1,h);
printw ("Cognome: %s\tNome: %s\n",osp->surn,osp->name);
osp->next=make_structure();
osp=osp->next; …Run Code Online (Sandbox Code Playgroud)