C语言 基于文件的通讯录管理系统。(文件中已有部分联系人信息信息)通讯录个人信息包括姓名、性别、年龄、电话、地址。用文件读/写操作编程实现通讯录的增加、查找、显示、删除、修改功能,完成通讯录编程、成功运行。
时间: 2023-12-22 22:24:16 浏览: 70
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 15
#define MAX_ADDR_LEN 50
typedef struct {
char name[MAX_NAME_LEN+1];
char gender;
int age;
char phone[MAX_PHONE_LEN+1];
char address[MAX_ADDR_LEN+1];
} Contact;
void print_menu();
void add_contact(FILE *fp);
void search_contact(FILE *fp);
void display_all_contacts(FILE *fp);
void delete_contact(FILE *fp);
void modify_contact(FILE *fp);
int main() {
FILE *fp;
char filename[] = "contacts.dat";
int choice;
fp = fopen(filename, "rb+");
if (fp == NULL) {
fp = fopen(filename, "wb+");
if (fp == NULL) {
printf("Failed to create file %s.\n", filename);
return -1;
}
}
while (1) {
print_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
add_contact(fp);
break;
case 2:
search_contact(fp);
break;
case 3:
display_all_contacts(fp);
break;
case 4:
delete_contact(fp);
break;
case 5:
modify_contact(fp);
break;
case 6:
fclose(fp);
return 0;
default:
printf("Invalid choice.\n");
}
}
}
void print_menu() {
printf("\n");
printf("1. Add a contact.\n");
printf("2. Search a contact.\n");
printf("3. Display all contacts.\n");
printf("4. Delete a contact.\n");
printf("5. Modify a contact.\n");
printf("6. Exit.\n");
printf("\n");
printf("Enter your choice: ");
}
void add_contact(FILE *fp) {
Contact contact;
printf("Enter the contact's name: ");
scanf("%s", contact.name);
printf("Enter the contact's gender (M/F): ");
scanf(" %c", &contact.gender);
printf("Enter the contact's age: ");
scanf("%d", &contact.age);
printf("Enter the contact's phone number: ");
scanf("%s", contact.phone);
printf("Enter the contact's address: ");
scanf("%s", contact.address);
fwrite(&contact, sizeof(Contact), 1, fp);
printf("Contact added successfully.\n");
}
void search_contact(FILE *fp) {
char name[MAX_NAME_LEN+1];
Contact contact;
int found = 0;
printf("Enter the name of the contact you want to search for: ");
scanf("%s", name);
rewind(fp);
while (fread(&contact, sizeof(Contact), 1, fp) == 1) {
if (strcmp(contact.name, name) == 0) {
printf("Name: %s\n", contact.name);
printf("Gender: %c\n", contact.gender);
printf("Age: %d\n", contact.age);
printf("Phone: %s\n", contact.phone);
printf("Address: %s\n", contact.address);
found = 1;
break;
}
}
if (!found) {
printf("Contact not found.\n");
}
}
void display_all_contacts(FILE *fp) {
Contact contact;
printf("%-20s %-10s %-5s %-15s %-50s\n", "Name", "Gender", "Age", "Phone", "Address");
rewind(fp);
while (fread(&contact, sizeof(Contact), 1, fp) == 1) {
printf("%-20s %-10c %-5d %-15s %-50s\n", contact.name, contact.gender, contact.age, contact.phone, contact.address);
}
}
void delete_contact(FILE *fp) {
char name[MAX_NAME_LEN+1];
Contact contact;
FILE *tmp;
int found = 0;
printf("Enter the name of the contact you want to delete: ");
scanf("%s", name);
tmp = fopen("temp.dat", "wb");
rewind(fp);
while (fread(&contact, sizeof(Contact), 1, fp) == 1) {
if (strcmp(contact.name, name) != 0) {
fwrite(&contact, sizeof(Contact), 1, tmp);
} else {
found = 1;
}
}
fclose(fp);
fclose(tmp);
remove("contacts.dat");
rename("temp.dat", "contacts.dat");
fp = fopen("contacts.dat", "rb+");
if (found) {
printf("Contact deleted successfully.\n");
} else {
printf("Contact not found.\n");
}
}
void modify_contact(FILE *fp) {
char name[MAX_NAME_LEN+1];
Contact contact;
int found = 0;
printf("Enter the name of the contact you want to modify: ");
scanf("%s", name);
rewind(fp);
while (fread(&contact, sizeof(Contact), 1, fp) == 1) {
if (strcmp(contact.name, name) == 0) {
printf("Enter the new phone number: ");
scanf("%s", contact.phone);
fseek(fp, -sizeof(Contact), SEEK_CUR);
fwrite(&contact, sizeof(Contact), 1, fp);
found = 1;
break;
}
}
if (found) {
printf("Contact modified successfully.\n");
} else {
printf("Contact not found.\n");
}
}
阅读全文