Contact Management System Program in c language with source code

Last Updated: 2022-10-09 09:56:50

contact management system in c

Contact Management System Program In C Language With Source Code

In this post, we will learn about how to create a contact management system program in c language. Also, you can download the complete source code and watch the youtube video so that you can learn the technique in the best way.

What Is Contact Management System?

A system that is used to keep contact information in organized way. It is like your phone book, so a contact management system is a system where you can store your all contact information, can edit and delete also you can search for contact information.

Use Of Contact Management System.

The main use of a contact management program is to organize contact information. For example

✅ Store all of your client's contact information.  
✅ You can send them a quick response and get back to your communication history.  
✅ You can set automated remainder.  
✅ You can set a schedule.

Contact Management System Using C Programming Language

To learn the c programming language we need to do some projects, and the Contact Management system is one of the easy projects you can create using the c programming language. You can also do some other projects like

  1. Student information management system in the c programming language.
  2. Employee information management system in the c programming language.
  3. Library management system in the c programming language.
  4. Login and Registration system in the c programming language.

To improve your implementation skills in the c programming language.

Topic Of C Programming Language To Create Contact Management System Project.

Almost we need all of the common topics of the c programming language to create the contact management system program. But let's highlight some of the topics why actually we need to those about this project.

Infinity Loop in C Programming Language

I think you already have knowledge about the loop in the c programming language, So now you need to know how to create and stop an infinite loop. We actually need this idea to show the menu utility a user wants to exit the program.

Switch Case in C programming Language

You may already have knowledge about the if-else clause in the C-Programming language. Now you need to know about the Switch Case Syntex in the C-Programming language which is much similar to the if-else clause. In the Contact management system program, we need this technology to create the menu. From there the user can select the action item he wants to perform.

Functions in C Programming Language

Functions are one of the most common important topics in the c programming language. I think you already know that. So you show know how to pass and return a string in a function in the c programming language. In our project for every operation, we will use this technology.

Text File Read and Write in the C-Programming Language.

Reading and Writing in a text file in C-programming language is one of the important things you should know to create this project. In our project, we will store all of our data in the text file also we will show all data from the text file so you should have an idea of how to Write and Read in a text file using the c-programming language.

Some Built-in functions we have used to create this project.

In our project, we have used some C-Language built-in functions. Like

  • strlen(): To calculate the length of a String
  • strcmp(): To compare two strings
  • itoa(): To convert an integer number to a string

Which types of data we will store to create the contact management system?

You may already understand that this will be a simple c-programming project. So we will not shote huge data, we will store some common data like

  • Concat Name
  • Contact Number
  • Contact Email

Features of Contact Management System Project

You may already know what can be done in this project, so here is a list so that you can understand in a moment what can be done in this contact management system.

✅ You can store new contact information.  
✅ You can show all the contact information together.  
✅ You can search for contact information from many numbers.  
✅ You can edit the contact information.  
✅ You can delete contact information.  
✅ You can delete all the contact information once.  
How Does The Menu Work?

To create menus, by normally printing a string in a formatted way and then we have called the function in an infinity loop so that the menu shows repeatedly until the user exit the program.

void Menu(){
    printf("\n\t **** My-Contact Management System ****\n");
    printf("\n\n\n\t\t\tMAIN MENU\n");
    printf("\t\t=====================\n");
    printf("\t\t[1] Add a new Contact\n");
    printf("\t\t[2] List all Contacts\n");
    printf("\t\t[3] Search for contact\n");
    printf("\t\t[4] Edit a Contact\n");
    printf("\t\t[5] Delete a Contact\n");
    printf("\t\t[6] Delete All Contact\n");
    printf("\t\t[7] Clear Window\n");
    printf("\t\t[i] User Guideline\n");
    printf("\t\t[a] About Us\n");
    printf("\t\t[0] Exit\n\t\t=================\n\t\t");
    printf("Enter the choice:");
}

Please download the complete source code for a better understanding.

 

while(isRunning == true){
    Menu();
    switch(option){
        case '0':
            Exit();
            break;
        case '1':
            AddNewContact();
            break;
        case '2':
            ShowAllContacts();
            break;
        case '3':
            SearchContact();
            break;
        case '4':
            EditContact();
            break;
        case '5':
            DeleteContact();
            break;
        case '6':
            DeleteAllContacts();
            break;
        case '7':
            system("cls");
            break;
        case 'I':
        case 'i':
            UserGuideline();
            break;
        case 'A':
        case 'a':
            AboutUs();
            break;
        default:
            ErrorAndRestart("Option not found!");
            break;
    }
}

Please download the complete source code for a better understanding.

How To Store The New Contact information?

You already know that will store a contact’s name, phone number, and email address. So we will take input all these things from the user as strings and save each thing in our text file. While taking input, we will keep in mind some validation rules like

✅ A name should be unique and cannot be more than twenty characters long.  
✅ A phone number should be unique and cannot be more than twenty characters long.  
✅ A Email address should be unique and must be a maximum of 30 characters.

One thing to keep in mind is that we will save each item as a new line, such as the name in one line, the phone number in the second line, and the email address will be stored in the third line.

 

void AddNewContact(){
    char Name[20];
    char Phone[20];
    char Email[30];
    char NewContact[100];
    NewContact[0] = 0;

    printf("Enter The Name: ");
    scanf(" %[^\n]s",Name);
    printf("Enter The Phone Number: ");
    scanf("%s",Phone);
    printf("Enter The Email: ");
    scanf("%s",Email);

    // creating a new contact
    strcat(NewContact,Name);
    strcat(NewContact,"\n");
    strcat(NewContact,Phone);
    strcat(NewContact,"\n");
    strcat(NewContact,Email);
    strcat(NewContact,"\n");

    FILE *allContactTxtFile = fopen("All-Contact.txt","a"); // open file
    fprintf(allContactTxtFile,NewContact); // store contact in text file
    fclose(allContactTxtFile);
    printf("\nContact Added Successfully!\n");
}

Please download the complete source code for a better understanding.

How To Show All The Numbers Together?

It is very easy to show all the contact numbers. We will read our text file where we stored our contact information. We will print all lines until the file reading is finished. We should know that a contact number consists of three numbers which means 1st line is a Name, 2nd line is a Phone, and 3rd line is an Email address, at the time of printing the line, we will follow this arrangement.

In our project, we have very nicely shown all the numbers one by one in a table(watch the video). To do this we have formatted our string which means we have added some space at the end of each string. For example, if a user inputs a name of eight characters, then we add twelve more spaces to this name and make it twenty. Similarly, we have done this for phone numbers and email addresses so that everything is always the same length.

void ShowAllContacts(){
    FILE* AllContactTextFile;
    int LineLength = 300;
    char Line[LineLength];

    // table header name = 20 phone = 20 and email = 30 characters
    printf("|====================|====================|==============================|\n");
    printf("|        Name        |    Phone Number    |          Email               |\n");
    printf("|====================|====================|==============================|\n");

    AllContactTextFile = fopen("All-Contact.txt", "r"); // read file
    int TotalContact = 0;
    int LineCount = 0;
    while(fgets(Line, LineLength, AllContactTextFile)) {
        LineCount++;
        if(LineCount==1){
            PrintLineWithSpace(Line,'n'); // this function will print name in formated way
        }else if(LineCount == 2){
            PrintLineWithSpace(Line,'p'); // this function will print phone in formated way
        }else if(LineCount == 3){
            PrintLineWithSpace(Line,'e'); // this function will print email in formated way
            LineCount=0;
            TotalContact++;
        }
    }
    printf("You Have Total %d Contacts.\n\n",TotalContact);
    fclose(AllContactTextFile); // cloase the file
}

By downloading the source code, you can easily understand the matter.

Search A Contact Number Out Of Many Contact Numbers

To find out a contact number from many contact numbers, we will search by name. Here, this search is very easy. If you understand how we showed all numbers from our text file then you will understand this easily. We will take out all the lines one by one from our text file. And if the first line of contact number matches the given name to search, then we can understand that there is a contact number for this name.

void SearchContact(){
    int FoundContact = 0;
    char Name[100];

    printf("Enter The Name: ");
    scanf(" %[^\n]s",Name);
    strcat(Name,"\n"); // adding new line char with name

    FILE* AllContactFile;
    int LineLength = 255;
    char Line[LineLength];
    AllContactFile = fopen("All-Contact.txt", "r"); // open file in read mood
    int LineCount = 0;
    while(fgets(Line, LineLength, AllContactFile)) {
        LineCount++;
        if(LineCount == 1 && strcmp(Name, Line) == 0){
            FoundContact = 1; 
        }
        if(FoundContact > 0){
            if(LineCount==1){
                printf("\nContact information of %s",Name);
                printf("\nName: %s",Line);
            }else if(LineCount == 2){
                printf("Phone: %s",Line);
            }else if(LineCount == 3){
                printf("Email: %s\n",Line);
            }
        }
        if(LineCount == 3){
            LineCount=0;
        }
    }
    fclose(AllContactFile);
    if(FoundContact == 0){
        printf("\nSorry no contact found for %s\n",Name);
    }
}

Please download the complete source code for a better understanding.

How To Edit A Contact Number?

Editing a contact number is a bit technical, if we look closely then we will find that our purpose is to give the user a chance to input three new lines and keep remain same all other lines.  
In the same way, like searching for a contact, the user will first give the name of the contact number he wants to edit. And we will continue to search line by line in our file. Whenever a line matches the contact name given by the user, we will ask the user to renew that line and the next two(phone and email) lines and all other lines will remain the same.  
Now we will use a little ingenuity to put all these things together. We will copy everything from our existing file(All-Numbers.txt) to a temporary file(Temp-All-Numbers.txt) with our three new lines. That means our temporary file will contain all the previous lines and the updated lines. Now, all we have to do is delete the existing file(All-Numbers.txt) and rename the temporary file(Temp-All-Numbers.txt) to the existing file name so that the temporary file will become the original file.  
Rename the Temp-All-Numbers.txt to All-Numbers.txt

Another excellent system we've seen here is if the user does not want to edit a line he can easily skip the line by entering the zero(0).

void EditContact(){
    int LineCount = 0;
    int FoundContact = 0;
    int SkipLines = 0;
    char GivenName[100];
    char NewName[100];
    char NewPhone[20];
    char NewEmail[100];
    char NewFullContact[300];
    NewFullContact[0] = 0;

    printf("Enter The Name which one you want to edit: ");
    scanf(" %[^\n]s",GivenName);
    strcat(GivenName,"\n");

    FILE* OurExistingFile;
    FILE* NewTempFile;
    int LineLength = 255;
    char Line[LineLength];
    OurExistingFile = fopen("All-Contact.txt", "r"); // open in read mood
    NewTempFile = fopen("temp-file.txt", "w"); // open in write mood + create the temp file
    while(fgets(Line, LineLength, OurExistingFile)) {
        LineCount++;

        if(LineCount == 1 && strcmp(GivenName, Line) == 0){
            // we found the contact
            FoundContact = 1;
            SkipLines = 3;
        }

        if(SkipLines > 0){
            if(LineCount == 1){
                printf("Old Name is: %s",Line);
                printf("New Name(0 for skip): ");
                scanf(" %[^\n]s",NewName);
                if(strcmp(NewName, "0") == 0){
                    strcat(NewFullContact,Line);
                }else{
                    strcat(NewFullContact,NewName);
                    strcat(NewFullContact,"\n");
                }
                SkipLines = 2;
            }else if(LineCount == 2){
                printf("Old Phone is: %s",Line);
                printf("New Phone(0 for skip): ");
                scanf("%s",NewPhone);
                if(strcmp(NewPhone, "0") == 0){
                    strcat(NewFullContact,Line);
                }else{
                    strcat(NewFullContact,NewPhone);
                    strcat(NewFullContact,"\n");
                }
                SkipLines = 1;
            }else if(LineCount == 3){
                printf("Old Email is: %s",Line);
                printf("New Email(0 for skip): ");
                scanf("%s",NewEmail);

                if(strcmp(NewEmail, "0") == 0){
                    strcat(NewFullContact,Line);
                }else{
                    strcat(NewFullContact,NewEmail);
                    strcat(NewFullContact,"\n");
                }
                SkipLines = 0;
                fprintf(NewTempFile,NewFullContact);
            }
        }else{
            fprintf(NewTempFile,Line);
        }

        if(LineCount == 3){
            LineCount = 0;
        }
    }
    fclose(NewTempFile);
    fclose(OurExistingFile);

    if(FoundContact == 0){
        printf("Contact Not Found!\n");
        remove("temp-file.txt");
    }else{
        printf("\nContact Updated Successfully!\n\n");
        remove("All-Contact.txt"); // delete existing file
        rename("temp-file.txt", "All-Contact.txt"); // renaim temp file to the name of existing file
    }
}

Please download the complete source code and watch the video for a better understanding.

How Can I Delete A Contact Number?

If you understand how to edit contacts, you will easily understand how to delete contacts. In our editing technique, we moved everything from our existing file to a temporary file and took three new lines. Now for deleting a contact number, we will do the same thing. We will bring everything from our existing file to the temporary file except for three new lines. And finally, we will delete our existing file and then rename the temporary file as an Existing file just to line the editing technique.

void DeleteContact(){
    int lineCount = 0;
    int FoundTheContact = 0;
    int SkipLines = 0;
    char GivenName[100];

    printf("Enter The Name which one you want to delete: ");
    scanf(" %[^\n]s",GivenName);
    strcat(GivenName,"\n");

    FILE* OurExistingFile;
    FILE* NewTempFile;
    int LineLenght = 300;
    char line[LineLenght];
    OurExistingFile = fopen("All-Contact.txt", "r"); // open in read mood
    NewTempFile = fopen("temp-file.txt", "w"); // open in write mood + create the file
    while(fgets(line, LineLenght, OurExistingFile)) {
        lineCount++;

        if(lineCount == 1 && strcmp(GivenName, line) == 0){
            // we found the contact
            FoundTheContact = 1;
            SkipLines = 3;
        }

        if(SkipLines > 0){
            SkipLines--;
        }else{
            fprintf(NewTempFile,line);
        }

        if(lineCount == 3){
            lineCount = 0;
        }
    }
    fclose(NewTempFile);
    fclose(OurExistingFile);

    if(FoundTheContact == 0){
        printf("\nContact Not Found!\n\n");
        remove("temp-file.txt");
    }else{
        printf("\nContact Deleted Successfully!\n\n");
        remove("All-Contact.txt"); // delete the existing file
        rename("temp-file.txt", "All-Contact.txt"); // rename the temp-file to the existing file name
    }
}

Please download the complete source code and watch the video for a better understanding.

How To Delete All Contact Numbers At Once?

It is very easy to delete all the contact numbers at once. By deleting the file where we have saved all our contact numbers you can delete all contact numbers at once.

void DeleteAllContacts(){
    system("cls");
    printf("\n\t\t **** Delete All The Contacts ****\n\n");

    char Option;
    getchar();
    printf("Are you sure want to delete all the contacts? (Y,N)?: ");
    scanf("%c",&Option);
    if(Option=='Y' || Option=='y'){
        int i;
        remove("All-Contact.txt"); // delete the file
        FILE *AllContactTxtFile = fopen("All-Contact.txt","a"); // create the file again
        fclose(AllContactTxtFile);
        printf("\nSuccessfully Deleted All Contacts!\n\n");
    }
}

Conclusion

If you know how to read and write lines in a text file with C programming then you can create a contact management system project easily. You can download the source code for understanding.

Still you face problems, feel free to contact with me, I will try my best to help you.