home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1998 March / pcx19_9803.iso / PC-XUSER / PC-XUSER.14 / ABC / TK0.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-07  |  3.8 KB  |  203 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. #define FILENAME "tk.dat"
  7.  
  8.   typedef struct {
  9.                    char   nev[30];
  10.                    char   szam[15];
  11.                    char   cim[30];
  12.                    char   reserved;
  13.                  } ADAT;
  14.  
  15.  
  16. FILE *datfile;
  17.  
  18. void bill();
  19.  
  20. void main(void)
  21. {
  22.  char c;
  23.  void uj ();
  24.  void lista ();
  25.  long keres ();
  26.  void modosit ();
  27.  int openfile(char *filename);
  28.  
  29.  clrscr();
  30.  if (openfile(FILENAME))
  31.  {
  32.   do
  33.   {
  34.     clrscr();
  35.     printf("Fomenu:\n");
  36.     printf(
  37.     "\n1 - uj szam"
  38.     "\n2 - listazas"
  39.     "\n3 - kereses"
  40.     "\n4 - modositas"
  41.     "\n0 - kilepes");
  42.     switch(c=getch())
  43.     {
  44.       case '1' : uj();                   break;
  45.       case '2' : lista();                break;
  46.       case '3' : keres();              break;
  47.       case '4' : modosit();              break;
  48.       case '0' : printf("\n\n C  S  A  !");      break;
  49.     }   // switch
  50.     fflush(datfile);
  51.   } while(c!='0');
  52.   fclose(datfile);
  53.  }
  54. }  // main
  55.  
  56. int openfile(char *filename)
  57. {
  58.  if ((datfile=fopen(filename,"r+"))==NULL)
  59.  {
  60.   printf("Az adatfile nem letzik, letrehozok egy masikat...");
  61.   bill();
  62.   if ((datfile=fopen(filename,"w"))==NULL)
  63.   {
  64.    printf("Nem tudom letrehozni az adatfile-t (lehet hogy CD-rol inditottal)");
  65.    return(0);
  66.   }
  67.   fclose(datfile);
  68.   datfile=fopen(filename,"r+");
  69.  }
  70.  return(1);
  71. }
  72.  
  73. void uj()
  74. {
  75.  ADAT rek;
  76.  
  77.  fseek(datfile,0,SEEK_END);
  78.  do
  79.  {
  80.   clrscr();
  81.   printf("Kerem az adatokat\n\n");
  82.   printf("Nev : ");
  83.   fflush(stdin);
  84.   gets(rek.nev);
  85.   printf("Telefonszam : ");
  86.   fflush(stdin);
  87.   gets(rek.szam);
  88.   printf("Lakcim : ");
  89.   fflush(stdin);
  90.   gets(rek.cim);
  91.   rek.reserved=0;
  92.   fwrite(&rek, sizeof(ADAT), 1, datfile);
  93.   printf("\n\nVan meg adat? (i/n) ");
  94.  } while(tolower(getch())!='n');
  95. }   // uj
  96.  
  97. void lista()
  98. {
  99.  ADAT rek;
  100.  int y=2;
  101.  
  102.  clrscr();
  103.  printf("   nev                            telefonszam                  Lakcim\n");
  104.  fseek(datfile,0,SEEK_SET);
  105.  fread(&rek,sizeof(ADAT),1,datfile);
  106.  while(!feof(datfile))
  107.  {
  108.   gotoxy(1,y);
  109.   printf("%s",rek.nev);
  110.   gotoxy(40-strlen(rek.szam)/2,y);
  111.   printf("%s",rek.szam);
  112.   gotoxy(80-strlen(rek.cim),y);
  113.   printf("%s",rek.cim);
  114.   fread(&rek,sizeof(ADAT),1,datfile);
  115.   if (y==24)
  116.   {
  117.    bill();
  118.    clrscr();
  119.    printf("   nev                        telefonszam                     Lakcim/n");
  120.    y=2;
  121.   } else y++;
  122.  }
  123.  bill();
  124. }     // lista
  125.  
  126. long keres()
  127. {
  128.  ADAT rek;
  129.  int nincs;
  130.  long i;
  131.  char string[30];
  132.  
  133.  clrscr();
  134.  printf("Kerem a nevet! ");
  135.  fflush(stdin);
  136.  gets(string);
  137.  nincs=1;
  138.  
  139.  clrscr();
  140.  fseek(datfile,0,SEEK_SET);
  141.  fread(&rek,sizeof(ADAT),1,datfile);
  142.  for(i=0;!feof(datfile) && nincs;i++)
  143.  {
  144.   if(strcmp(string,rek.nev)==0)
  145.   {
  146.    printf("Nev: %s\n",rek.nev);
  147.    printf("Telefonszam: %s\n",rek.szam);
  148.    printf("Lakcim: %s\n",rek.cim);
  149.    nincs=0;
  150.   }  // if
  151.   fread(&rek,sizeof(ADAT),1,datfile);
  152.  }   // for
  153.  if(nincs)
  154.  {
  155.   printf("\n Ilyen nevet nem talatam.");
  156.   bill();
  157.   return(-1);
  158.  }
  159.  bill();
  160.  return(i-1);
  161. }   // kereses
  162.  
  163. void modosit()
  164. {
  165.  ADAT rek;
  166.  long poz;
  167.  
  168.  clrscr();
  169.  fflush(stdin);
  170.  if ((poz=keres())!=-1)
  171.  {
  172.   poz*=sizeof(ADAT);
  173.   printf("Kerem az uj nevet:\n");
  174.   fflush(stdin);
  175.   gets(rek.nev);
  176.   printf("Kerem az uj szamot:\n");
  177.   fflush(stdin);
  178.   gets(rek.szam);
  179.   printf("Kerem az uj cimet:\n");
  180.   fflush(stdin);
  181.   gets(rek.cim);
  182.   rek.reserved=0;
  183.   fseek(datfile, poz, SEEK_SET);
  184.   fwrite(&rek, sizeof(ADAT), 1, datfile);
  185.   printf("modositas megtortent");
  186.   bill();
  187.  }
  188. }   // modositas
  189.  
  190. void bill()
  191. {
  192.  int x,y;
  193.  
  194.  x=wherex();
  195.  y=wherey();
  196.  gotoxy(1,25);
  197.  printf("nyomj meg egy gombot....");
  198.  getch();
  199.  gotoxy(1,25);
  200.  clreol();
  201.  gotoxy(x,y);
  202. }     // bill
  203.