c语言通讯录怎么保存上一次的记录,这个通讯录程序怎么save了n个数据load后就变成n+1个了...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

getch();

return(0);

}

p=stu->head;

clrscr();

printf("\n\nThere are all %d datas in this DateBase.\n\n",stu->length);

printf("%-15s","index");

printf("%-15s","Name");

printf("%-15s","Phone");

printf("%-15s\n","postcost");

i=1;

do

{

printf("%-15d",i++);

printf("%-15s",p->data.name);

printf("%-15s",p->data.phone);

printf("%-15s\n",p->data.postcost);

p=p->next;

j--;

}while(j!=0);

printf("\nPress any key to continue.");

getch();

}

Load (struct adLinklist *stu)

{

FILE *fp;

char fname[10];

struct dNode *temp;

printf("\nEnter the filename you want to load:");

gets(fname);

if((fp=fopen(fname,"rb"))==NULL)

{

printf("\nCan't Load the file.Please check the filename.");

getch();

return(0);

}

temp=(struct dNode*)malloc(sizeof(struct dNode));

fread(temp,sizeof(struct dNode),1,fp);

temp->prior=temp;

temp->next=temp;

stu->head=temp;

stu->rear=temp;

stu->length=1;

while(!feof(fp))

{

temp=(struct dNode*)malloc(sizeof(struct dNode));

fread(temp,sizeof(struct dNode),1,fp);

temp->prior=stu->rear;

temp->next=stu->head;

stu->rear->next=temp;

stu->rear=temp;

stu->head->prior=stu->rear;

stu->length++;

}

printf("\nLoad successful! Press any to continue.");

fclose(fp);

getch();

}

Save(struct adLinklist *stu)

{

struct dNode *temp1;

char fname[10];

FILE *fp1;

int i;

i=stu->length;

if(i==0)

{

printf("\nError! DateBase is empty! ENTER new data or LOAD DateBase from file.");

getch();

return(0);

}

printf("\nEnter the filename you want to save:");

gets(fname);

if((fp1=fopen(fname,"wb"))==NULL)

{

printf("Can't Save the file\n");

return(0);

}

temp1=stu->head;

while(i>0)

{

fwrite(temp1,sizeof(struct dNode),1,fp1);

temp1=temp1->next;

i--;

}

printf("Save succesfull.Press any key to continue.");

getch();

fclose(fp1);

}

Help()

{

clrscr();

printf("\n\n                             HELP About the Programme        \n\n");

printf("               **************************************************\n");

printf("               *   1.ENTER a new data to current DateBase       *\n");

printf("               *   2.DELETE a data from the current DateBase    *\n");

printf("               *   3.SEARCH a data of the current DateBase      *\n");

printf("               *   4.LIST all  data of the current DateBase     *\n");

printf("               *   5.SAVE the current database to HardDisk      *\n");

printf("               *   6.LOAD a  database from the HardDisk         *\n");

printf("               *   7.EXIT the Programe                          *\n");

printf("               *   8.HELP about this programme                  *\n");

printf("               **************************************************\n\n");

printf("                                 CopyRight @ AUST DCST 02-6 WuTao\n");

printf("                                               All Rights Reseved\n");

printf("                                                       2004.12.28\n\n\n");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个能保存数据通讯录程序示例,使用 C 语言编写: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_CONTACTS 100 // 定义联系人结构体 typedef struct { char name[50]; char phone[20]; char email[50]; } Contact; // 定义联系人数组和联系人数 Contact contacts[MAX_CONTACTS]; int num_contacts = 0; // 保存联系人函数 void save_contacts() { FILE *fp; fp = fopen("contacts.txt", "w"); if (fp == NULL) { printf("Error: could not open file.\n"); return; } for (int i = 0; i < num_contacts; i++) { fprintf(fp, "%s|%s|%s\n", contacts[i].name, contacts[i].phone, contacts[i].email); } fclose(fp); printf("Contacts saved successfully!\n"); } // 加载联系人函数 void load_contacts() { FILE *fp; fp = fopen("contacts.txt", "r"); if (fp == NULL) { printf("No contacts found.\n"); return; } char line[100]; while (fgets(line, 100, fp) != NULL) { char *name = strtok(line, "|"); char *phone = strtok(NULL, "|"); char *email = strtok(NULL, "|"); Contact c; strcpy(c.name, name); strcpy(c.phone, phone); strcpy(c.email, email); contacts[num_contacts++] = c; } fclose(fp); printf("Contacts loaded successfully!\n"); } // 添加联系人函数 void add_contact() { Contact c; printf("Enter name: "); scanf("%s", c.name); printf("Enter phone: "); scanf("%s", c.phone); printf("Enter email: "); scanf("%s", c.email); contacts[num_contacts++] = c; printf("Contact added successfully!\n"); } // 显示联系人列表函数 void list_contacts() { if (num_contacts == 0) { printf("No contacts found.\n"); } else { printf("Name\t\tPhone\t\tEmail\n"); for (int i = 0; i < num_contacts; i++) { printf("%s\t%s\t%s\n", contacts[i].name, contacts[i].phone, contacts[i].email); } } } // 主函数 int main() { int choice; load_contacts(); while (1) { printf("\n"); printf("1. Add contact\n"); printf("2. List contacts\n"); printf("3. Save contacts\n"); printf("4. Quit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: add_contact(); break; case 2: list_contacts(); break; case 3: save_contacts(); break; case 4: printf("Goodbye!\n"); save_contacts(); exit(0); default: printf("Invalid choice.\n"); } } return 0; } ``` 这个程序在原有的基础上增加了两个函数:`save_contacts()` 和 `load_contacts()`。`save_contacts()` 函数将所有联系人信息写入一个名为 "contacts.txt" 的文件中,`load_contacts()` 函数从该文件中读取联系人信息并将其存储在联系人数组中。 在主函数中,程序在启动时调用 `load_contacts()` 函数来加载之前保存的联系人数据。当用户选择退出程序时,程序会自动调用 `save_contacts()` 函数来保存当前的联系人数据。 这样,即使程序关闭,也可以保存之前添加的联系人数据,下次启动程序时可以继续使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值