Advertisement
LuandaBernardo

FILE RETRY

Jan 23rd, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct { char nome [30]; int tfixo, celular;
  6. }TContato;
  7.  
  8. TContato novocontato ()
  9. {
  10.     TContato contato;
  11.     printf ("Nome:");
  12.     fflush (stdin);
  13.     gets (contato.nome);
  14.     printf ("Telefone fixo:");
  15.     scanf ("%d", &contato.tfixo);
  16.     printf ("Celular:");
  17.     scanf ("%d", &contato.celular);
  18.     return contato;
  19. }
  20.  
  21. void adicaoContato (FILE *a)
  22. { TContato contato; int i=0;
  23. fseek (a, 0, SEEK_END);
  24. contato = novocontato();
  25. fwrite (&contato,sizeof (TContato),1, a);
  26.                                
  27. }
  28.  
  29.  
  30. void buscacontato (FILE *a, int num)
  31. {
  32.     TContato contato;
  33.     int i=0;
  34.     rewind (a);
  35.     while (!feof(a)){
  36.         fread (&contato,sizeof (TContato), 1,a);
  37.         if (contato.tfixo == num || contato.celular ==num)
  38.         {
  39.             printf ("Contato Encontrado!!!");
  40.             printf ("Nome: %s \n Telefone Fixo: %d \n Celular: %d", contato.nome, contato.tfixo, contato.celular);
  41.             i=1;
  42.         }}
  43.     {
  44.    
  45.         if  (i==0)
  46.         printf ("Contato NAO ENCONTRADO!!!");
  47.         getch();
  48.     }
  49.    
  50. }
  51.  
  52.  
  53. main ()
  54. {
  55.    
  56.     FILE *a;
  57.     int op; int num;
  58.      a = fopen("contatos.bin", "a+b");
  59.      if (a==NULL)
  60.     { printf ("ERRO AO ABRIR ARQUIVO!!!");}
  61.     else
  62.     printf ("\n \t \t \a MENU DE OPCOES \n \n 1 - CADASTRAR CONTATO \n 2 - LISTAR CONTATO POR NUMERO \n 3 - SAIR");
  63.     scanf ("%d", &op);
  64.    
  65.     switch (op)
  66.     {
  67.         case 1:
  68.         adicaoContato(a);
  69.         break;
  70.         case 2: printf ("Digite o numero para busca: "); scanf ("%d", &num);
  71.         buscacontato (a, num);
  72.         case 3:
  73.             return 0; break;
  74.             default: return 0;
  75.            
  76.     }
  77.     fclose (a);
  78.     getche ();
  79.     return 0;
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement