Contacts Manager – Mini Project in C with source code
destination source:https://www.programming-techniques.com/?p=2404
This is one of the mini-project that you can get started with C projects. The project is Contacts Manager using a C programming language. It is done using structure and control statements in C. It is a simple project made using the console application of C. This means, no graphics component is added. The main target user of this project is the C beginners who want to make the project in C programming language and especially those who are interested in learning the Class in C language.
Features of the mini-project are as below:-
- Add a New Contact
- List all contacts
- Search for contact
- Edit a contact
- Delete a contact
The complete source code for the project is given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | /********************************************************************************** Program Name : Contacts Manager Language Used : C ***********************************************************************************/ #include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> #include<stdlib.h> #include<dos.h> struct contact { long ph; char name[20],add[20],email[30]; }list; char query[20],name[20]; FILE *fp, *ft; int i,n,ch,l,found; int main() { main: system("cls"); /* ************Main menu *********************** */ printf("\n\t **** Welcome to contact Manager ****"); printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add a new Contact\n\t\t[2] List all Contacts\n\t\t[3] Search for contact\n\t\t[4] Edit a Contact\n\t\t[5] Delete a Contact\n\t\t[0] Exit\n\t\t=================\n\t\t"); printf("Enter the choice:"); scanf("%d",&ch); switch(ch) { case 0: printf("\n\n\t\tAre you sure u want to exit?"); break; /* *********************add new contacts************ */ case 1: system("cls"); fp=fopen("contact.dll","a"); for (;;) { fflush(stdin); printf("To exit enter blank space in the name input\nName (Use identical):"); scanf("%[^\n]",&list.name); if(stricmp(list.name,"")==0 || stricmp(list.name," ")==0) break; fflush(stdin); printf("Phone:"); scanf("%ld",&list.ph); fflush(stdin); printf("address:"); scanf("%[^\n]",&list.add); fflush(stdin); printf("email address:"); gets(list.email); printf("\n"); fwrite(&list,sizeof(list),1,fp); } fclose(fp); break; /* *********************list of contacts************************* */ case 2: system("cls"); printf("\n\t\t================================\n\t\t\tLIST OF CONTACTS\n\t\t================================\n\nName\t\tPhone No\t Address\t\tE-mail ad.\n=================================================================\n\n"); for(i=97;i<=122;i=i+1) { fp=fopen("contact.dll","r"); fflush(stdin); found=0; while(fread(&list,sizeof(list),1,fp)==1) { if(list.name[0]==i || list.name[0]==i-32) { printf("\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n",list.name, list.ph,list.add,list.email); found++; } } if(found!=0) { printf("=========================================================== [%c]-(%d)\n\n",i-32,found); getch(); } fclose(fp); } break; /* *******************search contacts********************** */ case 3: system("cls"); do { found=0; printf("\n\n\t..::CONTACT SEARCH\n\t===========================\n\t..::Name of contact to search: "); fflush(stdin); scanf("%[^\n]",&query); l=strlen(query); fp=fopen("contact.dll","r"); system("cls"); printf("\n\n..::Search result for '%s' \n===================================================\n",query); while(fread(&list,sizeof(list),1,fp)==1) { for(i=0;i<=l;i++) name[i]=list.name[i]; name[l]='\0'; if(stricmp(name,query)==0) { printf("\n..::Name\t: %s\n..::Phone\t: %ld\n..::Address\t: %s\n..::Email\t:%s\n",list.name,list.ph,list.add,list.email); found++; if (found%4==0) { printf("..::Press any key to continue..."); getch(); } } } if(found==0) printf("\n..::No match found!"); else printf("\n..::%d match(s) found!",found); fclose(fp); printf("\n ..::Try again?\n\n\t[1] Yes\t\t[0] No\n\t"); scanf("%d",&ch); }while(ch==1); break; /* *********************edit contacts************************/ case 4: system("cls"); fp=fopen("contact.dll","r"); ft=fopen("temp.dat","w"); fflush(stdin); printf("..::Edit contact\n===============================\n\n\t..::Enter the name of contact to edit:"); scanf("%[^\n]",name); while(fread(&list,sizeof(list),1,fp)==1) { if(stricmp(name,list.name)!=0) fwrite(&list,sizeof(list),1,ft); } fflush(stdin); printf("\n\n..::Editing '%s'\n\n",name); printf("..::Name(Use identical):"); scanf("%[^\n]",&list.name); fflush(stdin); printf("..::Phone:"); scanf("%ld",&list.ph); fflush(stdin); printf("..::address:"); scanf("%[^\n]",&list.add); fflush(stdin); printf("..::email address:"); gets(list.email); printf("\n"); fwrite(&list,sizeof(list),1,ft); fclose(fp); fclose(ft); remove("contact.dll"); rename("temp.dat","contact.dll"); break; /* ********************delete contacts**********************/ case 5: system("cls"); fflush(stdin); printf("\n\n\t..::DELETE A CONTACT\n\t==========================\n\t..::Enter the name of contact to delete:"); scanf("%[^\n]",&name); fp=fopen("contact.dll","r"); ft=fopen("temp.dat","w"); while(fread(&list,sizeof(list),1,fp)!=0) if (stricmp(name,list.name)!=0) fwrite(&list,sizeof(list),1,ft); fclose(fp); fclose(ft); remove("contact.dll"); rename("temp.dat","contact.dll"); break; default: printf("Invalid choice"); break; } printf("\n\n\n..::Enter the Choice:\n\n\t[1] Main Menu\t\t[0] Exit\n"); scanf("%d",&ch); switch (ch) { case 1: goto main; case 0: break; default: printf("Invalid choice"); break; } return 0; } |
Related Article
destination source:https://www.programming-techniques.com/?p=2404