Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BIOS ProgrammingRSS Feeds

Program of telephone directory that provides append, find and display all records from a file

Posted By: Easy Tutor     Category: C Programming     Views: 11162

Write a program of telephone directory that provides below facility using file.

1) Add and Append records
2) Search / Find
3) Display all records

Code for Program of telephone directory that provides append, find and display all records from a file in C Programming

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

struct person{
     char name[20];
     long telno;
};

void appendData(){
      FILE *fp;
      struct person obj;
      clrscr();
      fp=fopen("data.txt","a");
      printf("*****Add Record****\n");
      printf("Enter Name : ");
      scanf("%s",obj.name);
      printf("Enter Telephone No. : ");
      scanf("%ld",&obj.telno);
      fprintf(fp,"%20s %7ld",obj.name,obj.telno);
      fclose(fp);
}

void showAllData(){
      FILE *fp;
      struct person obj;
      clrscr();
      fp=fopen("data.txt","r");
      printf("*****Display All Records*****\n");
      printf("\n\n\t\tName\t\t\tTelephone No.");
      printf("\n\t\t=====\t\t\t===============\n\n");
      while(!feof(fp))
      {
      fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
      printf("%20s %30ld\n",obj.name,obj.telno);
      }
      fclose(fp);
      getch();
}

void findData(){
      FILE *fp;
      struct person obj;
      char name[20];
      int totrec=0;
      clrscr();
      fp=fopen("data.txt","r");
      printf("*****Display Specific Records*****\n");
      printf("\nEnter Name : ");
      scanf("%s",&name);
      while(!feof(fp))
      {
      fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
         if(strcmpi(obj.name,name)==0){
        printf("\n\nName   :  %s",obj.name);
        printf("\nTelephone No : %ld",obj.telno);
        totrec++;
         }
      }
      if(totrec==0)
         printf("\n\n\nNo Data Found");
      else
         printf("\n\n===Total %d Record found===",totrec);
      fclose(fp);
      getch();
}



void main(){
      char choice;
      while(1){
    clrscr();
    printf("*****TELEPHONE DIRECTORY*****\n\n");
    printf("1) Append Record\n");
    printf("2) Find Record\n");
    printf("3) Read all record\n");
    printf("4) exit\n");
    printf("Enter your choice : ");
    fflush(stdin);
    choice = getche();
    switch(choice){
         case'1' : //call append record
            appendData();
            break;
         case'2' : //call find record
             findData();
            break;
         case'3' : //Read all record
            showAllData();
            break;
         case'4' :
         case 27  : exit(1);
    }
      }
}
  
Share: 



Easy Tutor
Easy Tutor author of Program of telephone directory that provides append, find and display all records from a file is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!