A telephone directory implemented in C

Source: Internet
Author: User
Tags definition exit continue fread printf strcmp

A phone number management program, in VC + + 6.0 through.

#include <stdio.h>
#include <string.h>
struct Fileh//************************************* definition structure (file information buffer)
{
Long FC;
}FH;
struct Telsys//********************************* definition structure (file data buffer)
{
Char usename[30];
Char useadd[50];
Long Usetel;
}TEL[200];


void AddNew (); Defining function types
void Del ();
void display ();
void edit ();
void FindName ();
void Findtel ();
void Quit ();
void Qingli ();
void Qingli ()//************************************* information file Cleanup (scavenging of garbage data) √
{
Long i=0;
FILE *f;
if ((F=fopen ("Telefon.dat", "RB")) ==null)
{
printf ("Open file failed!") The system is about to exit. \ n ");
Getch ();
Exit (0);
}
Fseek (f,0,0);
Fread (&fh,sizeof (struct Fileh), 1,f);
while (I&LT;FH.FC)
{
Fseek (f,sizeof (struct Fileh) + (i) *sizeof (struct Telsys), 0);
Fread (&tel[i],sizeof (struct telsys), 1,f);
i++;
}
Fclose (f);

i=0;
if ((F=fopen ("Telefon.dat", "wb+")) ==null)
{
printf ("Open file failed!") The system is about to exit. \ n ");
Getch ();
Exit (0);
}
while (I&LT;FH.FC)
{
Fseek (f,0,0);
Fwrite (&fh,sizeof (struct Fileh), 1,f);
Fseek (f,sizeof (struct Fileh) + (i) *sizeof (struct Telsys), 0);
Fwrite (&tel[i],sizeof (struct telsys), 1,f);
i++;
}
Fclose (f);
printf ("\n\n\n\t\t\t\t file cleaned up!") ");
}
void del ()//*************************************** delete function √
{
Long i=0,in=0;
Char ch;
FILE *f;
if ((F=fopen ("Telefon.dat", "rb+")) ==null)
{
printf ("Open file failed!") The system is about to exit. \ n ");
Getch ();
Exit (0);
}
Fseek (f,0,0);
Fread (&fh,sizeof (struct Fileh), 1,f);
if (fh.fc>0)
{
Todo
{
Fseek (f,0,0);
Fread (&fh,sizeof (struct Fileh), 1,f);
Display ();
Todo
{
printf ("Please enter the data to be deleted serial number: \ n");
scanf ("%ld", &in);
if (in<=0)
printf ([note]: Please enter a number greater than 0!) \ n ");
}while (in<=0);
if (IN&LT;=FH.FC)
{
I=in;
while (I&LT;=FH.FC)
{
Fseek (f,sizeof (struct Fileh) + (i) *sizeof (struct Telsys), 0);
Fread (&tel[i],sizeof (struct telsys), 1,f);
strcpy (Tel[i-1].usename,tel[i].usename);
strcpy (Tel[i-1].useadd,tel[i].useadd);
Tel[i-1].usetel=tel[i].usetel;
i++;
}
fh.fc--;
Fseek (f,0,0);
Fwrite (&fh,sizeof (struct Fileh), 1,f);
I=in;
while (I&LT;=FH.FC)
{
Fseek (f,sizeof (struct Fileh) + (i-1) *sizeof (struct Telsys), 0);
Fwrite (&tel[i-1],sizeof (struct telsys), 1,f);
i++;
}
printf ("Delete message complete!") ");
}
Else
{
printf ("No information of this serial number!") (-_-;)");
}
if (fh.fc>0)
{
printf (Do you want to continue modifying?) (y/n): ");
Ch=getch ();
}
Else
{
Ch= ' n ';
printf ("No data to delete!") ");
Getch ();
}
}while (ch== ' y ') | | ch== ' Y ');
}
Else
{
printf ("\n\n\n\n\n\n\t\t\t\t has no information to delete!") ");
Getch ();
}
Fclose (f);
}
void Findtel ()//*********************************** by number lookup function √
{
Long i=0,in=0,pan=0,panyiyang=0,panduan=0;
FILE *f;
if ((F=fopen ("Telefon.dat", "RB")) ==null)
{
printf ("Open file failed!") The system is about to exit. \ n ");
Getch ();
Exit (0);
}
Fseek (f,0,0);
Fread (&fh,sizeof (struct Fileh), 1,f);
if (fh.fc>0)
{
Fseek (f,sizeof (struct Fileh), 0);
Fread (&tel,fh.fc*sizeof (struct telsys), 1,f);
Todo
{
printf ("\n\n\n\n\n\n\t\t\t Please enter the phone number you want to find:");
scanf ("%ld", &in);
if (in<4000000| | in>4999999)
printf ("\t\t\t[Note]: Please enter the number 4000000 to 4999999!" \ n ");
}while (in<4000000| | in>4999999);
while (I&LT;FH.FC)
{
Fseek (f,sizeof (struct Fileh) + (i) *sizeof (struct Telsys), 0);
Fread (&tel[i],sizeof (struct telsys), 1,f);
if (tel[i].usetel==in)
{
panyiyang++;
}
i++;
}
if (panyiyang!=0)
{
System ("CLS");
printf ("╔══╦══════════╦═══════════════╦════╗\n");
printf ("║ serial number ║ Subscriber name ║ subscriber address ║ phone ║\n");
printf ("╚══╩══════════╩═══════════════╩════╝\n");
printf ("┌──┬──────────┬───────────────┬────┐\n");
i=0;
while (I&LT;FH.FC)
{
if (tel[i].usetel==in)
{
panduan++;
printf ("│%-4d│%-20s│%-30s│%-8ld│\n", I+1,tel[i].usename,tel[i].useadd,tel[i].usetel);
if (Panduan!=panyiyang)
printf ("├──┼──────────┼───────────────┼────┤\n");
}
i++;
}
printf ("└──┴──────────┴───────────────┴────┘\n");
}
Else
{
printf ("\n\t\t\t not Found!") \ n ");
}
}
Else
{
printf ("\n\n\n\n\n\n\t\t\t\t has no information to find!") ");
}
Fclose (f);
Getch ();
}
void FindName ()//********************************** lookup function by name
{
Long i=0,pan=0,panyiyang=0,panduan=0;
Char in[30];
FILE *f;
if ((F=fopen ("Telefon.dat", "RB")) ==null)
{
printf ("Open file failed!") The system is about to exit. \ n ");
Getch ();
Exit (0);
}
Fseek (f,0,0);
Fread (&fh,sizeof (struct Fileh), 1,f);
if (fh.fc>0)
{
Fseek (f,sizeof (struct Fileh), 0);
Fread (&tel,fh.fc*sizeof (struct telsys), 1,f);
printf ("\n\n\n\n\n\n\t\t\t Please enter the Subscriber name to find:");
scanf ("%s", &in);
while (I&LT;FH.FC)
{
if (strcmp (tel[i].usename,in) ==0)
{
panyiyang++;
}
i++;
}
if (panyiyang!=0)
{
System ("CLS");
printf ("╔══╦══════════╦═══════════════╦════╗\n");
printf ("║ serial number ║ Subscriber name ║ subscriber address ║ phone ║\n");
printf ("╚══╩══════════╩═══════════════╩════╝\n");
printf ("┌──┬──────────┬───────────────┬────┐\n");
i=0;
while (I&LT;FH.FC)
{
if (strcmp (tel[i].usename,in) ==0)
{
panduan++;
printf ("│%-4d│%-20s│%-30s│%-8ld│\n", I+1,tel[i].usename,tel[i].useadd,tel[i].usetel);
if (Panduan!=panyiyang)
printf ("├──┼──────────┼───────────────┼────┤\n");
}
i++;
}
printf ("└──┴──────────┴───────────────┴────┘\n");
}
Else
{
printf ("\n\t\t\t not Found!") \ n ");
}
}
Else
{
printf ("\n\n\n\n\n\n\t\t\t\t has no information to find!") ");
}
Fclose (f);
Getch ();
}
void edit ()//************************************** Modify function √
{
Long in=0;
Char ch;
FILE *f;
if ((F=fopen ("Telefon.dat", "rb+")) ==null)
{
printf ("Open file failed!") The system is about to exit. \ n ");
Getch ();
Exit (0);
}
Fseek (f,0,0);
Fread (&fh,sizeof (struct Fileh), 1,f);
if (fh.fc>0)
{
Todo
{
Fseek (f,0,0);
Fread (&fh,sizeof (struct Fileh), 1,f);
Display ();
Todo
{
printf ("Please enter the data number to be modified: \ n");
scanf ("%ld", &in);
if (in<=0)
printf ([note]: Please enter a number greater than 0!) \ n ");
}while (in<=0);
if (IN&LT;=FH.FC)
{
printf ("Please enter your name:");
scanf ("%s", &tel[in-1].usename);
printf ("Please enter your address:");
scanf ("%s", &tel[in-1].useadd);
Todo
{
printf ("Please enter your phone number:");
scanf ("%ld", &tel[in-1].usetel);
if (tel[in-1].usetel<4000000| | tel[in-1].usetel>4999999)
printf ([note]: Please enter the number from 4000000 to 4999999! \ n ");
}while (tel[in-1].usetel<4000000| | tel[in-1].usetel>4999999);
Fseek (f,sizeof (struct Fileh) + (in-1) *sizeof (struct Telsys), 0);
Fwrite (&tel[in-1],sizeof (struct telsys), 1,f);
printf ("Modify information complete!") ");
}
Else
{
printf ("No information of this serial number!") (-_-;)");
}
printf (Do you want to continue modifying?) (y/n): ");
Ch=getch ();
}while (ch== ' y ') | | ch== ' Y ');
}
Else
{
printf ("\n\n\n\n\n\n\t\t\t\t has no information to modify!") ");
Getch ();
}
Fclose (f);
}
void display ()//********************************** view function √
{
Long i=0;
FILE *f;
if ((F=fopen ("Telefon.dat", "RB")) ==null)
{
printf ("Open file failed!") The system is about to exit. \ n ");
Getch ();
Exit (0);
}
System ("CLS");
Fseek (f,0,0);
Fread (&fh,sizeof (struct Fileh), 1,f);
if (fh.fc>0)
{
printf ("╔══╦══════════╦═══════════════╦════╗\n");
printf ("║ serial number ║ Subscriber name ║ subscriber address ║ phone ║\n");
printf ("╚══╩══════════╩═══════════════╩════╝\n");
printf ("┌──┬──────────┬───────────────┬────┐\n");
while (I&LT;FH.FC)
{
Fseek (f,sizeof (struct Fileh) + (i) *sizeof (struct Telsys), 0);
Fread (&tel[i],sizeof (struct telsys), 1,f);
printf ("│%-4d│%-20s│%-30s│%-8ld│\n", I+1,tel[i].usename,tel[i].useadd,tel[i].usetel);
if (i!=fh.fc-1)
printf ("├──┼──────────┼───────────────┼────┤\n");
if ((i+1)%10==0)//-------------------------------------------10 number Paging display
{Getch ();}
i++;
}
printf ("└──┴──────────┴───────────────┴────┘\n");
}
Else
{
printf ("\n\n\n\n\n\n\t\t\t\t has no information to show!") ");
}
Fclose (f);
}
void AddNew ()//************************************ new function √
{
Char ch;
FILE *f;

if ((F=fopen ("Telefon.dat", "rb+")) ==null)
{
printf ("\n\n\n\t\t\t\t Open file failed!") The system is about to exit. \ n ");
Getch ();
Exit (0);
}
Todo
{
Fseek (f,0,0);
Fread (&fh,sizeof (struct Fileh), 1,f);
if (fh.fc<200)
{
fh.fc++;
printf ("\n\n\n\t\t\t Please enter your name:");
scanf ("%s", &tel[fh.fc-1].usename);
printf ("\t\t\t Please enter your address:");
scanf ("%s", &tel[fh.fc-1].useadd);
Todo
{
printf ("\t\t\t Please enter your phone number:");
scanf ("%ld", &tel[fh.fc-1].usetel);
if (tel[fh.fc-1].usetel<4000000| | tel[0].usetel>4999999)
printf ("\t\t\t[Note]: Please enter the number 4000000 to 4999999!" \ n ");
}while (tel[fh.fc-1].usetel<4000000| | tel[0].usetel>4999999);
Fseek (f,0,0);
Fwrite (&fh,sizeof (struct Fileh), 1,f);
Fseek (f,sizeof (struct Fileh) + (fh.fc-1) *sizeof (struct Telsys), 0);
Fwrite (&tel[fh.fc-1],sizeof (struct telsys), 1,f);
printf ("\t\t\t Added information complete!") Do you want to continue filling? (y/n): ");
Ch=getch ();
}
Else
{
printf ("\t\t\t over the limit.") It can't be stored anymore! ");
Ch= ' n ';
}
}while (ch== ' y ') | | ch== ' Y ');
Fclose (f);
}
void Quit ()//************************************ Exit function
{
Qingli ();
printf ("\n\t\t\t Welcome to use this system!") \ n ');
printf ("\t\t\t Author: Jingi \ n");
printf ("\t\t\te-mail:jrg9522@126.com\n");
printf ("\t\t\tqq:70939594\n");
printf ("\t\t\t software nature: free software \ n");
printf ("\t\t\t modification Note: \ n");
printf ("\t\t\t\t this time to beautify the interface; \ n");
printf ("\t\t\t\t but user input error bug has not changed; \ n");
printf ("\t\t\t\t in the Chinese character input method input numbers and \n\t\t\t\t input in the way the number entered in fact \n\t\t\t\t different, the bug has not changed." ; \n\t\t\thelp me! ");
Getch ();
}
void Main ()//************************************ main function
{
Char ch;
FILE *f;
if ((F=fopen ("Telefon.dat", "AB")) ==null)
{
printf ("\n\n\n\t\t\t\t Open file failed!") The system is about to exit. \ n ");
Getch ();
Exit (0);
}
Fclose (f);
Todo
{
System ("CLS");
printf ("\n\t\t\t╔═══════════════╗\n");
printf ("\t\t\t║ phone number storage System v1.3║\n");
printf ("\t\t\t╚═══════════════╝\n");
printf ("\t\t\t┌───────────────┐\n");
printf ("\t\t\t│1. Add new phone information │\n");
printf ("\t\t\t├───────────────┤\n");
printf ("\t\t\t│2. Modify information │\n");
printf ("\t\t\t├───────────────┤\n");
printf ("\t\t\t│3. Delete Information │\n");
printf ("\t\t\t├───────────────┤\n");
printf ("\t\t\t│4. Find information by phone number │\n");
printf ("\t\t\t├───────────────┤\n");
printf ("\t\t\t│5. Find information by name │\n");
printf ("\t\t\t├───────────────┤\n");
printf ("\t\t\t│6. View information │\n");
printf ("\t\t\t├───────────────┤\n");
printf ("\t\t\t│7. Exit system │\n");
printf ("\t\t\t└───────────────┘\n");
printf ("\t\t\t, by Operation number:");
Ch=getch ();
Switch (CH)
{
Case ' 1 ':
System ("CLS");
AddNew ();
Break
Case ' 2 ':
System ("CLS");
Edit ();
Break
Case ' 3 ':
System ("CLS");
Del ();
Break
Case ' 4 ':
System ("CLS");
Findtel ();
Break
Case ' 5 ':
System ("CLS");
FindName ();
Break
Case ' 6 ':
Display ();
Getch ();
Break
Case ' 7 ':
System ("CLS");
Quit ();
Break
}
}while (ch!= ' 7 ');
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.