Address Book System in C (File Handling Example)
This project demonstrates an Address Book in C that allows you to add, display, search, update, and delete contacts. It uses structures and file handling to store and manage contact information efficiently.
1. Features
- Add new contacts
- Display all contacts
- Search a contact by name
- Update contact details
- Delete a contact
- Store contacts permanently using files
2. Structure Definition
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Contact {
char name[50];
char phone[15];
char email[50];
};
3. File-Based Operations
#define FILENAME "contacts.dat"
// Add contact
void addContact() {
struct Contact c;
FILE *fp = fopen(FILENAME, "ab");
if(!fp) { printf("Error opening file.\n"); return; }
printf("Enter Name: "); scanf(" %[^\n]", c.name);
printf("Enter Phone: "); scanf(" %[^\n]", c.phone);
printf("Enter Email: "); scanf(" %[^\n]", c.email);
fwrite(&c, sizeof(c), 1, fp);
fclose(fp);
printf("Contact added successfully!\n");
}
// Display all contacts
void displayContacts() {
struct Contact c;
FILE *fp = fopen(FILENAME, "rb");
if(!fp) { printf("No records found.\n"); return; }
printf("\nName\tPhone\tEmail\n");
while(fread(&c, sizeof(c), 1, fp)) {
printf("%s\t%s\t%s\n", c.name, c.phone, c.email);
}
fclose(fp);
}
// Search contact by name
void searchContact() {
char searchName[50];
struct Contact c;
printf("Enter Name to search: "); scanf(" %[^\n]", searchName);
FILE *fp = fopen(FILENAME, "rb");
if(!fp) { printf("No records found.\n"); return; }
int found = 0;
while(fread(&c, sizeof(c), 1, fp)) {
if(strcmp(c.name, searchName) == 0) {
printf("Name: %s\nPhone: %s\nEmail: %s\n", c.name, c.phone, c.email);
found = 1;
break;
}
}
if(!found) printf("Contact not found.\n");
fclose(fp);
}
// Update contact
void updateContact() {
char searchName[50];
struct Contact c;
FILE *fp = fopen(FILENAME, "rb+");
if(!fp) { printf("No records found.\n"); return; }
printf("Enter Name to update: "); scanf(" %[^\n]", searchName);
int found = 0;
while(fread(&c, sizeof(c), 1, fp)) {
if(strcmp(c.name, searchName) == 0) {
printf("Enter new Phone: "); scanf(" %[^\n]", c.phone);
printf("Enter new Email: "); scanf(" %[^\n]", c.email);
fseek(fp, -sizeof(c), SEEK_CUR);
fwrite(&c, sizeof(c), 1, fp);
printf("Contact updated successfully!\n");
found = 1;
break;
}
}
if(!found) printf("Contact not found.\n");
fclose(fp);
}
// Delete contact
void deleteContact() {
char searchName[50];
struct Contact c;
FILE *fp = fopen(FILENAME, "rb");
FILE *temp = fopen("temp.dat", "wb");
if(!fp || !temp) { printf("File error.\n"); return; }
printf("Enter Name to delete: "); scanf(" %[^\n]", searchName);
int found = 0;
while(fread(&c, sizeof(c), 1, fp)) {
if(strcmp(c.name, searchName) == 0) {
found = 1;
continue; // skip copying
}
fwrite(&c, sizeof(c), 1, temp);
}
fclose(fp);
fclose(temp);
remove(FILENAME);
rename("temp.dat", FILENAME);
if(found) printf("Contact deleted successfully!\n");
else printf("Contact not found.\n");
}
4. Main Menu
int main() {
int choice;
while(1) {
printf("\n--- Address Book ---\n");
printf("1. Add Contact\n2. Display Contacts\n3. Search Contact\n4. Update Contact\n5. Delete Contact\n6. Exit\n");
printf("Enter your choice: "); scanf("%d", &choice);
switch(choice) {
case 1: addContact(); break;
case 2: displayContacts(); break;
case 3: searchContact(); break;
case 4: updateContact(); break;
case 5: deleteContact(); break;
case 6: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}
5. Key Points to Remember
- Uses struct to store contact information
- File handling ensures permanent storage (
fwrite,fread) - Menu-driven program allows easy navigation
- Can be extended to include sorting contacts, searching by phone/email, or backup options