Introduction
In this blog post, we will walk through the process of creating a simple Contact Book application in C. We will use data structures to store contact information and file handling to save and retrieve contacts from a file. Additionally, we'll add error handling and data validation to make the program more robust.
Define the Contact Structure
We begin by defining a structure to represent a contact. Each contact will have a name, phone number, and email address.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 50
#define MAX_PHONE_LENGTH 15
#define MAX_EMAIL_LENGTH 50
struct Contact {
char name[MAX_NAME_LENGTH];
char phone[MAX_PHONE_LENGTH];
char email[MAX_EMAIL_LENGTH];
};
Implement File Handling Functions
We'll add functions to handle reading and writing contacts to a file. We'll use the "contacts.txt" file to store the contacts.
void addContact(struct Contact newContact) {
FILE *file = fopen("contacts.txt", "a");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
fprintf(file, "%s;%s;%s\n", newContact.name, newContact.phone, newContact.email);
fclose(file);
}
void displayContacts() {
FILE *file = fopen("contacts.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
struct Contact contact;
printf("\n--- Contacts ---\n");
while (fscanf(file, "%[^;];%[^;];%[^\n]\n", contact.name, contact.phone, contact.email) != EOF) {
printf("Name: %s\nPhone: %s\nEmail: %s\n\n", contact.name, contact.phone, contact.email);
}
fclose(file);
}
Implement Data Validation
To ensure the data entered by the user is valid, we'll create validation functions for each field (name, phone number, and email address).
int validate_name(const char name[]) {
// Perform any validation rules for the name, if needed
// For this example, we'll just check if it's not empty
return strlen(name) > 0;
}
int validate_phone(const char phone[]) {
// Perform any validation rules for the phone number, if needed
// For this example, we'll just check if it contains only digits
for (int i = 0; phone[i] != '\0'; i++) {
if (!isdigit(phone[i])) {
return 0;
}
}
return 1;
}
int validate_email(const char email[]) {
// Perform any validation rules for the email address, if needed
// For this example, we'll just check if it contains '@'
return (strstr(email, "@") != NULL);
}
Implement Main Menu
Now, let's create the main function to provide a user-friendly menu for interacting with the Contact Book.
void clear_input_buffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
}
int main() {
int choice;
struct Contact newContact;
char searchName[MAX_NAME_LENGTH];
while (1) {
printf("Contact Book Menu:\n");
printf("1. Add Contact\n");
printf("2. Display Contacts\n");
printf("3. Search Contact\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
clear_input_buffer();
switch (choice) {
case 1:
// ... Code for adding a contact ...
break;
case 2:
// ... Code for displaying contacts ...
break;
case 3:
// ... Code for searching a contact ...
break;
case 4:
printf("\nExiting the program.\n");
exit(0);
default:
printf("\nInvalid choice. Please try again.\n");
clear_input_buffer();
}
}
return 0;
}
Implement Add Contact Feature
In the case of adding a contact, we'll prompt the user to input the name, phone number, and email address. We'll then validate the data before adding the contact.
case 1:
printf("\nEnter contact name: ");
fgets(newContact.name, sizeof(newContact.name), stdin);
newContact.name[strcspn(newContact.name, "\n")] = '\0'; // Remove newline character
// Validate the name
if (!validate_name(newContact.name)) {
printf("Invalid name. Please try again.\n");
break;
}
printf("Enter contact phone number: ");
fgets(newContact.phone, sizeof(newContact.phone), stdin);
newContact.phone[strcspn(newContact.phone, "\n")] = '\0'; // Remove newline character
// Validate the phone number
if (!validate_phone(newContact.phone)) {
printf("Invalid phone number. Please enter only digits.\n");
break;
}
printf("Enter contact email address: ");
fgets(newContact.email, sizeof(newContact.email), stdin);
newContact.email[strcspn(newContact.email, "\n")] = '\0'; // Remove newline character
// Validate the email address
if (!validate_email(newContact.email)) {
printf("Invalid email address. Please enter a valid email.\n");
break;
}
addContact(newContact);
printf("Contact added successfully.\n");
break;
Implement Search Contact Feature
For searching for a contact, we'll prompt the user to input the name they want to search for. We'll then search for the contact in the file and display the results.
case 3:
printf("\nEnter the name to search: ");
fgets(searchName, sizeof(searchName), stdin);
searchName[strcspn(searchName, "\n")] = '\0'; // Remove newline character
searchContact(searchName);
break;
Conclusion
Congratulations! You've successfully built a basic Contact Book application in C with data structures and file handling. You've also added error handling and data validation to ensure that the user enters valid information.
From here, you can further expand and enhance the application, adding features like editing and deleting contacts, or implementing more sophisticated data validation rules.
Keep exploring and happy coding!