Search
  • English
Login Register
  • Mon - Sat 11.00 am - 8:00 pm
  • 1st-29, Atlanta Business Hub, VIP Road, Surat
  • +91 97129 28220
Code and Debug
  • Offline Courses
    • C Programming
    • Cpp Programming
    • Django Framework
    • Flutter Development
    • HTML & CSS
    • Javascript
    • MySQL
    • Node.js (Core)
    • Node.js (Advance)
    • Python Programming
  • About Us
  • Contact Us
  • Blog
  • Offline Courses
    • C Programming
    • Cpp Programming
    • Django Framework
    • Flutter Development
    • HTML & CSS
    • Javascript
    • MySQL
    • Node.js (Core)
    • Node.js (Advance)
    • Python Programming
  • About Us
  • Contact Us
  • Blog
Code and Debug > Blog > Project > C Project > Phonebook Management System in C

Phonebook Management System in C

  • November 18, 2022
  • Posted by: Code and Debug
  • Category: C Project Project
No Comments

In this blog you will be making your own Phone Book Management System using C Programming. Source code and explanation given below.

Whole program is divided into Functions!

Our whole C Program will be divided into 7 functions:

  • askPassword (Entry point where user is asked for password to enter our system)
  • printBars (To print beautiful bars at the start)
  • menu (Main logic where menu is printed and user selects the operation)
  • createContact (To create contact and save it in a file)
  • searchContact (To search particular contact and print the details)
  • listAllContacts (To list all contacts stored in our file)
  • deleteContact (To delete a contact by given name)

askPassword() function

Ask password will be called to ensure only authorized user enters our system. If correct password is entered, user will be shown the menu.

Code for askPassword() is given below:

				
					void askPassword()
{
	char password[20] = {"codeanddebug"};
	int i, l;
	char title[50] = "Authorized User Only";
	l = strlen(title);
	printBars();
	for (i = 0; i <= l; i++)
	{
		Sleep(60);
		printf(" %c", title[i]);
	}
	printBars();
	printf("\n\nEnter your password: ");
	char ch, pass[20];
	char w = '*';
	i = 0;
	while (ch != 13)
	{
		ch = getch(); // Getting character by character
		if (ch != 13 && ch != 8)
		{
			printf("%c", w); // Printing * when each character typed
			pass[i] = ch;	 // Storing that character in our pass
			i++;
		}
	}
	pass[i] = '\0'; // Ending password entered by user by NULL

	// Comparing Password
	if (strcmp(pass, password) == 0)
	{
		printf("\n\nPassword entered is correct.");
		Sleep(1500);
		menu(); // Then showing the menu
	}
	else
	{
		printf("\n\nPassword entered is incorrect.");
		Sleep(1500);
		system("cls"); // Clearing the whole screen
		askPassword(); // Then running our whole function again
	}
}
				
			

printBars() function

This will be used to print beautiful bars at the start.

				
					void printBars()
{
	// To print bars
	int i;
	for (i = 0; i <= 20; i++)
	{
		Sleep(60);
		printf("\xDB");
	}
}
				
			

menu() function

This function will be used to print whole menu where user will be shown bunch of operations and can select any one of them.

				
					void menu()
{
	int choice;
	system("cls");
	printf("---------------------MY PHONEBOOK DIRECTORY---------------------\n\n");
	printf("1. Add Contact\n");
	printf("2. Search Contact\n");
	printf("3. List all Contacts\n");
	printf("4. Delete Contact\n");
	printf("5. Exit\n");
	printf("Enter your choice = ");
	scanf("%d", &choice);

	// Starting switch case upon choice entered by user
	switch (choice)
	{
	case 1:
		createContact();
		break;
	case 2:
		searchContact();
		break;
	case 3:
		listAllContacts();
		break;
	case 4:
		deleteContact();
		break;
	case 5:
		system("cls");
		printf("\n-----------THANKYOU FOR USING PHONEBOOK.-----------\n\n\n\n");
		Sleep(1000);
		break;
	default:
		printf("Invalid Option choosen");
		Sleep(1500);
		system("cls");
		menu();
	}
}
				
			

createContact() function

This function will use to create new contact and save data in variables and then put it into the file.

				
					void createContact()
{
	system("cls");
	printf("---------------------CREATE NEW CONTACT---------------------\n\n");
	FILE *fptr; // Creating file pointer
	char name[100], address[100], email[100], gender[10];
	long long int phoneNumber;

	// Opening file in ab+ mode to help us
	// appending data and not overwriting them
	fptr = fopen("phonerecord.txt", "ab+");
	if (fptr == NULL)
	{
		printf("Cannot create phonerecord.txt. Please check permissions");
		fclose(fptr); // Closing the file
		Sleep(1000);
	}
	else
	{
		printf("Enter name -> ");
		scanf("%s", &name);
		printf("Enter %s's address ->", name);
		scanf("%s", &address);
		printf("Enter %s's gender -> ", name);
		scanf("%s", &gender);
		printf("Enter %s's email -> ", name);
		scanf("%s", &email);
		printf("Enter %s's phone number -> ", name);
		scanf("%lld", &phoneNumber);
		printf("Phone number %lld1", phoneNumber);

		// Adding data into file
		fprintf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, phoneNumber); // Appending data to file
		fclose(fptr);																	// Closing the file
		printf("\n\n\nContact added to record. Press ENTER to continue.");
		getch();
		system("cls");
	}
	menu();
}
				
			

searchContact() function

This function will use to search the name of contact and display the details reading from .txt file.

				
					void searchContact()
{
	FILE *fptr; // Creating file pointer
	int found = 0, result;
	char name[100], address[100], email[100], gender[10], searchName[100];
	long long int phoneNumber;
	system("cls");
	fflush(stdin);
	printf("Enter person name you want to search = ");
	scanf("%s", &searchName);
	fptr = fopen("phonerecord.txt", "r"); // Opening in read mode
	while (fscanf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, &phoneNumber) != EOF)
	{
		result = strcmp(name, searchName);
		// Comparing strings, if 0 means equal
		if (result == 0)
		{
			found = 1;
			printf("Name -> %s\n", name);
			printf("Address -> %s\n", address);
			printf("Gender -> %s\n", gender);
			printf("Email -> %s\n", email);
			printf("Phone number -> %d\n", phoneNumber);
			printf("\n-----------------\n");
		}
	}
	if (found == 0)
	{
		printf("Person does not exists.\n");
	}
	Sleep(1500);
	printf("\n\nPress ENTER to Continue");
	getch();
	menu();
}
				
			

listAllContacts() function

This function will use to display all the contacts in our phonebook.

				
					void listAllContacts()
{
	FILE *fptr; // Creating file pointer
	char name[100], address[100], email[100], gender[10];
	long long int phoneNumber;
	system("cls");
	fflush(stdin);
	fptr = fopen("phonerecord.txt", "r"); // Opening in read mode
	while (fscanf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, &phoneNumber) != EOF)
	{
		printf("Name -> %s\n", name);
		printf("Address -> %s\n", address);
		printf("Gender -> %s\n", gender);
		printf("Email -> %s\n", email);
		printf("Phone number -> %d\n", phoneNumber);
		printf("\n-------------\n");
	}
	printf("\n\nPress ENTER to go to main menu.");
	getch();
	menu();
}
				
			

deleteContact() function

This function will use to delete a record with name entered by user.

				
					void deleteContact()
{
	FILE *fptr, *fptr1; // Creating 2 files, one for read and other for write.
	char name[100], address[100], email[100], gender[10], searchName[100];
	int result, found = 0;
	long long int phoneNumber;
	fptr = fopen("phonerecord.txt", "r");	   // Reading
	fptr1 = fopen("phonerecordTEMP.txt", "a"); // Temporary file for writing data
	system("cls");
	printf("Enter the CONTACT name that you want to delete: ");
	scanf("%s", &searchName);
	system("cls");
	while (fscanf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, &phoneNumber) != EOF)
	{
		result = strcmp(name, searchName);
		if (result == 0)
		{
			found = 1;
			printf("Record deleted successfully");
		}
		else
		{
			fprintf(fptr1, "%s %s %s %s %lld\n", name, address, gender, email, phoneNumber);
		}
	}
	if (found == 0)
	{
		printf("Record Not found.");
	}
	fclose(fptr);
	fclose(fptr1);
	fptr = fopen("phonerecord.txt", "w");
	fclose(fptr);
	fptr = fopen("phonerecord.txt", "a");
	fptr1 = fopen("phonerecordTEMP.txt", "r");
	while (fscanf(fptr1, "%s %s %s %s %lld\n", name, address, gender, email, &phoneNumber) != EOF)
	{
		fprintf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, phoneNumber);
	}
	fclose(fptr);
	fclose(fptr1);
	fptr1 = fopen("phonerecordTEMP.txt", "w");
	fclose(fptr1);
	printf("\n\nPress ENTER for main menu\n");
	getch();
	menu();
}
				
			

main() function

The entry point where our code will start running.

				
					int main()
{
	askPassword();
}

				
			

Source Code

Below is the source code for our C Program.

				
					#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <dos.h>
#include <string.h>

void printBars()
{
	// To print bars
	int i;
	for (i = 0; i <= 20; i++)
	{
		Sleep(60);
		printf("\xDB");
	}
}

void searchContact()
{
	FILE *fptr; // Creating file pointer
	int found = 0, result;
	char name[100], address[100], email[100], gender[10], searchName[100];
	long long int phoneNumber;
	system("cls");
	fflush(stdin);
	printf("Enter person name you want to search = ");
	scanf("%s", &searchName);
	fptr = fopen("phonerecord.txt", "r"); // Opening in read mode
	while (fscanf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, &phoneNumber) != EOF)
	{
		result = strcmp(name, searchName);
		// Comparing strings, if 0 means equal
		if (result == 0)
		{
			found = 1;
			printf("Name -> %s\n", name);
			printf("Address -> %s\n", address);
			printf("Gender -> %s\n", gender);
			printf("Email -> %s\n", email);
			printf("Phone number -> %d\n", phoneNumber);
			printf("\n-----------------\n");
		}
	}
	if (found == 0)
	{
		printf("Person does not exists.\n");
	}
	Sleep(1500);
	printf("\n\nPress ENTER to Continue");
	getch();
	menu();
}

void listAllContacts()
{
	FILE *fptr; // Creating file pointer
	char name[100], address[100], email[100], gender[10];
	long long int phoneNumber;
	system("cls");
	fflush(stdin);
	fptr = fopen("phonerecord.txt", "r"); // Opening in read mode
	while (fscanf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, &phoneNumber) != EOF)
	{
		printf("Name -> %s\n", name);
		printf("Address -> %s\n", address);
		printf("Gender -> %s\n", gender);
		printf("Email -> %s\n", email);
		printf("Phone number -> %d\n", phoneNumber);
		printf("\n-------------\n");
	}
	printf("\n\nPress ENTER to go to main menu.");
	getch();
	menu();
}

void deleteContact()
{
	FILE *fptr, *fptr1; // Creating 2 files, one for read and other for write.
	char name[100], address[100], email[100], gender[10], searchName[100];
	int result, found = 0;
	long long int phoneNumber;
	fptr = fopen("phonerecord.txt", "r");	   // Reading
	fptr1 = fopen("phonerecordTEMP.txt", "a"); // Temporary file for writing data
	system("cls");
	printf("Enter the CONTACT name that you want to delete: ");
	scanf("%s", &searchName);
	system("cls");
	while (fscanf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, &phoneNumber) != EOF)
	{
		result = strcmp(name, searchName);
		if (result == 0)
		{
			found = 1;
			printf("Record deleted successfully");
		}
		else
		{
			fprintf(fptr1, "%s %s %s %s %lld\n", name, address, gender, email, phoneNumber);
		}
	}
	if (found == 0)
	{
		printf("Record Not found.");
	}
	fclose(fptr);
	fclose(fptr1);
	fptr = fopen("phonerecord.txt", "w");
	fclose(fptr);
	fptr = fopen("phonerecord.txt", "a");
	fptr1 = fopen("phonerecordTEMP.txt", "r");
	while (fscanf(fptr1, "%s %s %s %s %lld\n", name, address, gender, email, &phoneNumber) != EOF)
	{
		fprintf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, phoneNumber);
	}
	fclose(fptr);
	fclose(fptr1);
	fptr1 = fopen("phonerecordTEMP.txt", "w");
	fclose(fptr1);
	printf("\n\nPress ENTER for main menu\n");
	getch();
	menu();
}

void createContact()
{
	system("cls");
	printf("---------------------CREATE NEW CONTACT---------------------\n\n");
	FILE *fptr; // Creating file pointer
	char name[100], address[100], email[100], gender[10];
	long long int phoneNumber;

	// Opening file in ab+ mode to help us
	// appending data and not overwriting them
	fptr = fopen("phonerecord.txt", "ab+");
	if (fptr == NULL)
	{
		printf("Cannot create phonerecord.txt. Please check permissions");
		fclose(fptr); // Closing the file
		Sleep(1000);
	}
	else
	{
		printf("Enter name -> ");
		scanf("%s", &name);
		printf("Enter %s's address ->", name);
		scanf("%s", &address);
		printf("Enter %s's gender -> ", name);
		scanf("%s", &gender);
		printf("Enter %s's email -> ", name);
		scanf("%s", &email);
		printf("Enter %s's phone number -> ", name);
		scanf("%lld", &phoneNumber);
		printf("Phone number %lld1", phoneNumber);

		// Adding data into file
		fprintf(fptr, "%s %s %s %s %lld\n", name, address, gender, email, phoneNumber); // Appending data to file
		fclose(fptr);																	// Closing the file
		printf("\n\n\nContact added to record. Press ENTER to continue.");
		getch();
		system("cls");
	}
	menu();
}

void menu()
{
	int choice;
	system("cls");
	printf("---------------------MY PHONEBOOK DIRECTORY---------------------\n\n");
	printf("1. Add Contact\n");
	printf("2. Search Contact\n");
	printf("3. List all Contacts\n");
	printf("4. Delete Contact\n");
	printf("5. Exit\n");
	printf("Enter your choice = ");
	scanf("%d", &choice);

	// Starting switch case upon choice entered by user
	switch (choice)
	{
	case 1:
		createContact();
		break;
	case 2:
		searchContact();
		break;
	case 3:
		listAllContacts();
		break;
	case 4:
		deleteContact();
		break;
	case 5:
		system("cls");
		printf("\n-----------THANKYOU FOR USING PHONEBOOK.-----------\n\n\n\n");
		Sleep(1000);
		break;
	default:
		printf("Invalid Option choosen");
		Sleep(1500);
		system("cls");
		menu();
	}
}

void askPassword()
{
	char password[20] = {"codeanddebug"};
	int i, l;
	char title[50] = "Authorized User Only";
	l = strlen(title);
	printBars();
	for (i = 0; i <= l; i++)
	{
		Sleep(60);
		printf(" %c", title[i]);
	}
	printBars();
	printf("\n\nEnter your password: ");
	char ch, pass[20];
	char w = '*';
	i = 0;
	while (ch != 13)
	{
		ch = getch(); // Getting character by character
		if (ch != 13 && ch != 8)
		{
			printf("%c", w); // Printing * when each character typed
			pass[i] = ch;	 // Storing that character in our pass
			i++;
		}
	}
	pass[i] = '\0'; // Ending password entered by user by NULL

	// Comparing Password
	if (strcmp(pass, password) == 0)
	{
		printf("\n\nPassword entered is correct.");
		Sleep(1500);
		menu(); // Then showing the menu
	}
	else
	{
		printf("\n\nPassword entered is incorrect.");
		Sleep(1500);
		system("cls"); // Clearing the whole screen
		askPassword(); // Then running our whole function again
	}
}

int main()
{
	askPassword();
}

				
			

Screenshots

Below are some screenshots of the program.

Leave a Reply Cancel reply

About US

At Code & Debug, our mission is to continuously innovate the best ways to train the next generation of developers and to transform the the way tech education is delivered.

Code & Debug was founded in 2020 to bridge the knowledge gap between colleges and industry. Founded by Anirudh Khurana, Code & Debug has professional teaching faculty and a state-of-art learning platform for Coding education.
View Courses

Pages

  • About Us
  • Contact Us
  • Home
  • Offline Courses
  • User Account

Contact Us

  • 1st-29, Atlanta Business Hub, VIP Road, Surat
  • Tel.: +91 97129 28220
  • info@codeanddebug.in