Contact Management System In C Language

Prajukta Dey
4 min readDec 7, 2021

--

Part II

Firstly, thank you for the amazing response from your side. Here’s the long awaited part for Contact Management System in C Language Part II.

Before, you start reading this, I would suggest you go through the first part of this project. I am attaching the link below.

Now, without any further ado, let’s start with the second part of the project.

The parts that are still left for this project are:

  1. Creating a new contact.
  2. Listing all the contacts.
  3. Searching for a contact.
  4. Editing a contact.
  5. Deleting a contact.

For this part, I will be explaining the first 3 sections.

Creating a new contact:

The fopen() method in C is a library function that is used to open a file to perform various operations which include reading, writing etc. along with various modes. Here, we have used fopen() to open a .dat file named ‘contact.dat’. A file with the .dat extension is a generic data file that stores specific information relating to the program that created the file.

fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream). Then, we have printed a message which states “To exit enter a blank space in the name input”. If the user inputs a blank space, the program will terminate and will take the user out of the system.

stricmp() is a string comparison function. The function checks for a blank space in the input and if it finds one, the program is terminated. If not, the name is stored in the list named ‘list.name’.

Similarly, we have taken the input for phone number, address and email-id. You can add other fields if you want according to your own liking and necessities.

After, we have taken the input, we are going to print the “Contact Successfully Saved!” message for easy readability. Finally, we close the file by using fclose().

Output:

Listing all Contacts:

Line number 65 is not visible so I am attaching the statement below:

printf(“\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n”,list.name,list.ph,list.address,list.email);

system(“cls”) clears the screen. The for loop has a range of 97–122 which is the ascii value of lower case alphabets. Line 61 checks whether the file is readable and checks for its contents. Line 63 checks whether the name starts with a lowercase or uppercase and if the program finds something, it will display all contacts.

If we find a contact, it will display the contacts one by one. Remember to close the file once you are done working with it. We don’t want any unnecessary space complexity in our program.

Output:

The output above list all the contacts that we saved.

Searching for a contact:

Line 95 is: printf(“\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n”,list.name,list.ph,list.address,list.email);

To search a contact, we need to take in input the name of the contact we need to search. After receiving the input, we open the file ‘contact.dat’ and search for the contact by using the string function stricmp(). If the contact is found the the counter found increases by 1.

If the counter named found is 0 then it prints the statement ‘Contact not found.

Output:

That’s it for this part. I will be posting another article on this topic covering the last 2 sections soon. Stay tuned!

I am attaching my Github repository link to this project below.

Thank you for reading. Have a nice day!

--

--

Prajukta Dey

A student living in India currently into generative ai among many other things.