C 将结构保存到文件中

C 将结构保存到文件中,c,file,struct,C,File,Struct,我正在做一个程序,将联系人列表保存在一个带有structs的文件中。我已经尝试了很多方法,但是当我尝试向程序读取文件时,它什么都不读取 这是我的程序,没有打开文件并保存到文件: #include <stdio.h> #include <stdlib.h> struct agenda { int idContacte; char name[50]; struct agenda *nextContacte; }; struct agenda *pAg

我正在做一个程序,将联系人列表保存在一个带有structs的文件中。我已经尝试了很多方法,但是当我尝试向程序读取文件时,它什么都不读取

这是我的程序,没有打开文件并保存到文件:

#include <stdio.h>
#include <stdlib.h>

struct agenda {
    int idContacte;
    char name[50];
    struct agenda *nextContacte;
};
struct agenda *pAgenda;
struct agenda *pFirst = NULL;
struct agenda *pIndex;

void insert();
void show();

int main()
{
    //Menu
    int opc;
    while(1){
        printf("1.Insert Contact.\n");
        printf("2.Show Contacts.\n");
        printf("3.Exit\n");
        scanf("%d", &opc);
        switch(opc){
            case 1:
                insert();
                break;
            case 2:
                show();
                break;
            case 3:
                return 0;
        }
    }
}
void insert(){
    pAgenda = (struct agenda *)malloc(sizeof(struct agenda));
    printf("Insert ID: ");
    scanf("%d", &pAgenda->idContacte);
    printf("Insert the name: ");
    scanf("%s", pAgenda->name);
    printf("\n");
    if (pFirst==NULL || pAgenda->idContacte < pFirst->idContacte)
    {
        pAgenda->nextContacte=pFirst;
        pFirst=pAgenda;
    }
    else if (pAgenda->idContacte > pFirst->idContacte){
        pIndex=pFirst;
        while(pIndex->nextContacte && pIndex->nextContacte->idContacte < pAgenda->idContacte)
        {
            pIndex = pIndex->nextContacte;
        }
        pAgenda->nextContacte = pIndex->nextContacte;
        pIndex->nextContacte = pAgenda;
    }
}
void show(){
    pIndex = pFirst;
    while(pIndex && pIndex->idContacte <= 100) {
        printf("\nID: %d", pIndex->idContacte);
        printf("\nNAME: %s", pIndex->name);
        printf("\n\n");
        pIndex = pIndex->nextContacte;
    }
}
#包括
#包括
结构议程{
int-idContacte;
字符名[50];
结构议程*nextContacte;
};
结构议程*pAgenda;
结构议程*pFirst=NULL;
结构议程*pIndex;
无效插入();
void show();
int main()
{
//菜单
int-opc;
而(1){
printf(“1.插入联系人。\n”);
printf(“2.Show Contacts.\n”);
printf(“3.退出\n”);
scanf(“%d”和&opc);
交换机(opc){
案例1:
插入();
打破
案例2:
show();
打破
案例3:
返回0;
}
}
}
空白插入(){
pAgenda=(结构议程*)malloc(结构议程的大小);
printf(“插入ID:”);
scanf(“%d”,&pAgenda->idcontact);
printf(“插入名称:”);
scanf(“%s”,pAgenda->name);
printf(“\n”);
如果(pFirst==NULL | | pAgenda->idcontactidcontact)
{
pAgenda->nextContacte=pFirst;
pFirst=pAgenda;
}
否则如果(pAgenda->idContacte>pFirst->idContacte){
pIndex=pFirst;
同时(pIndex->nextcontact&&pIndex->nextcontact->idcontactidcontact)
{
pIndex=pIndex->nextContacte;
}
pAgenda->nextContacte=pIndex->nextContacte;
pIndex->nextContacte=pAgenda;
}
}
无效显示(){
pIndex=pFirst;
while(pIndex&&pIndex->idcontact-idcontact);
printf(“\n名称:%s”,索引->名称);
printf(“\n\n”);
pIndex=pIndex->nextContacte;
}
}

您能帮助我如何在程序开始时从文件中获取联系人,然后在插入联系人时,重写文件并将所有联系人再次插入文件中吗?

当您结束程序时,应执行以下操作

int save_list(struct agenda *head) {
  FILE *save = fopen("file.name", "wb");
  if(!save) return -1;

  while(head) {
    fwrite(head, sizeof *head - sizeof head, 1, save);
    head = head->nextContacte;
  }

  fclose(save);
  /* Somebody would free list memory after this function execution */
  return 0;
}
struct agenda *restore_list() {
  FILE *restore= fopen("file.name", "rb");
  struct agenda *head = NULL;
  struct agenda *cur = head;
  struct agenda temp;
  if(!restore) return head;

  while( fwrite(&temp, sizeof temp - sizeof head, 1, save) == 1) {
    struct agenda *node = malloc( sizeof(struct agenda) );
    if(NULL == node) {
      /* Handle out of memory error here, free list */
      return NULL;
    }
    *node = temp;
    node->nextContacte = NULL;
    if(head) {
      cur->nextContacte = node;
      cur = node;
    } else {
       /* First node */
       head = cur = node;
    }
  }

  fclose(restore);
  return head;
}
在程序开始时,您应该执行以下操作

int save_list(struct agenda *head) {
  FILE *save = fopen("file.name", "wb");
  if(!save) return -1;

  while(head) {
    fwrite(head, sizeof *head - sizeof head, 1, save);
    head = head->nextContacte;
  }

  fclose(save);
  /* Somebody would free list memory after this function execution */
  return 0;
}
struct agenda *restore_list() {
  FILE *restore= fopen("file.name", "rb");
  struct agenda *head = NULL;
  struct agenda *cur = head;
  struct agenda temp;
  if(!restore) return head;

  while( fwrite(&temp, sizeof temp - sizeof head, 1, save) == 1) {
    struct agenda *node = malloc( sizeof(struct agenda) );
    if(NULL == node) {
      /* Handle out of memory error here, free list */
      return NULL;
    }
    *node = temp;
    node->nextContacte = NULL;
    if(head) {
      cur->nextContacte = node;
      cur = node;
    } else {
       /* First node */
       head = cur = node;
    }
  }

  fclose(restore);
  return head;
}

没有可读取的代码???也许您的意思是
struct agenda*restore_list(){
fread
而不是
fwrite
在同一个函数中。@RSahu谢谢你。很好。我会按照你的建议更新。@harper我明白了你的意思。我发布了这段代码,相信这是在
C
中序列化/反序列化链接列表的通用算法。以后回答时我会更加小心。