Introduction
In this tutorial, we will create a Contact Book application in C programming language. This application will allow users to store, view, and manage their contact information efficiently. You will learn how to work with structures, functions, and basic file handling to store contacts. This is a beginner-friendly project to help you understand basic C concepts.
Objective
The objective of this program is to create a simple and functional contact management system. The program will:
- Store contact details such as name, phone number, and email.
- Display the contact list.
- Allow adding and deleting contacts.
- Provide an option to search for a contact by name.
Code
#include #include #include // Define the structure for a contact struct Contact { char name[50]; char phone[15]; char email[50]; }; // Function to add a contact void addContact() { struct Contact newContact; FILE *file = fopen("contacts.dat", "ab"); if (!file) { printf("Could not open file.\n"); return; } printf("Enter name: "); getchar(); // To consume any leftover newline character from previous input fgets(newContact.name, sizeof(newContact.name), stdin); newContact.name[strcspn(newContact.name, "\n")] = '\0'; // Remove trailing newline printf("Enter phone number: "); fgets(newContact.phone, sizeof(newContact.phone), stdin); newContact.phone[strcspn(newContact.phone, "\n")] = '\0'; printf("Enter email: "); fgets(newContact.email, sizeof(newContact.email), stdin); newContact.email[strcspn(newContact.email, "\n")] = '\0'; fwrite(&newContact, sizeof(struct Contact), 1, file); fclose(file); printf("Contact added successfully.\n"); } // Function to display all contacts void displayContacts() { struct Contact contact; FILE *file = fopen("contacts.dat", "rb"); if (!file) { printf("No contacts found.\n"); return; } printf("\nContacts List:\n"); while (fread(&contact, sizeof(struct Contact), 1, file)) { printf("Name: %s\nPhone: %s\nEmail: %s\n\n", contact.name, contact.phone, contact.email); } fclose(file); } // Function to search for a contact void searchContact() { char searchName[50]; struct Contact contact; int found = 0; printf("Enter name to search: "); getchar(); // To consume any leftover newline character fgets(searchName, sizeof(searchName), stdin); searchName[strcspn(searchName, "\n")] = '\0'; FILE *file = fopen("contacts.dat", "rb"); if (!file) { printf("Could not open file.\n"); return; } while (fread(&contact, sizeof(struct Contact), 1, file)) { if (strcmp(contact.name, searchName) == 0) { printf("Contact found!\n"); printf("Name: %s\nPhone: %s\nEmail: %s\n", contact.name, contact.phone, contact.email); found = 1; break; } } if (!found) { printf("Contact not found.\n"); } fclose(file); } // Main function to run the application int main() { int choice; while (1) { printf("\nContact Book Application\n"); printf("1. Add Contact\n"); printf("2. Display All Contacts\n"); printf("3. Search Contact by Name\n"); printf("4. 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: printf("Exiting application...\n"); exit(0); default: printf("Invalid choice! Please try again.\n"); } } return 0; }
Explanation of the Program
The Contact Book program is divided into several key sections:
- Structures: The `struct Contact` is defined to hold the details of each contact, such as name, phone number, and email.
- Functions: The program defines three key functions:
- addContact(): Adds a new contact to the `contacts.dat` file.
- displayContacts(): Displays all the stored contacts from the file.
- searchContact(): Searches for a contact by name in the stored contacts.
- Main function: The `main()` function is the entry point of the program, presenting the user with a menu to perform different actions.
How to Run the Program
- Save the code in a file named
contact_book.c
. - Open a terminal or command prompt and navigate to the directory containing the file.
- Compile the program using a C compiler (e.g.,
gcc contact_book.c -o contact_book
). - Run the program using
./contact_book
(on Linux/macOS) orcontact_book.exe
(on Windows).
The program will run in a loop, allowing you to add, display, or search contacts until you choose to exit the program.
Dear Sir/Madam,
we specialize in connecting our investors with high-quality, revenue-generating companies seeking
funding across various stages and sectors. Further details regarding the capital funding and requirements
can be shared upon your confirmation of interest. Please let us know if you would like to proceed
with a discussion. If this opportunity is not of interest to you, no further action is required.
Best regards,
RB
Vert & M | Outside Sales Representative