Advertisement
roby45alfa

Rubrica AGGIORNATO [23_09_2017]

Sep 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. // Rubrica.cpp : definisce il punto di ingresso dell'applicazione console.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. typedef struct {
  8.     char Cognome[30];
  9.     char Telefono[30];
  10.     char M_F;
  11. }Rubrica;
  12. int menu(char *NFile);
  13. void getRubrica(Rubrica *p);
  14. int main()
  15. {
  16.     FILE *f;
  17.     char NFile[30] = { "\0" };
  18.  
  19.     while (1)
  20.     {
  21.         Rubrica p1;
  22.         switch (menu(NFile))
  23.         {
  24.         case 1:
  25.             printf("Nome File: ");
  26.             scanf("%s", NFile);
  27.             while (getchar() != '\n');
  28.             if (!(f = fopen(NFile, "a+")))
  29.             {
  30.                 printf("Errore!\n");
  31.                 exit(1);
  32.             }
  33.             break;
  34.         case 2:
  35.             if (!(f = fopen(NFile, "r")))
  36.             {
  37.                 printf("Errore!\n");
  38.                 exit(1);
  39.             }
  40.             while (fscanf(f, "%s %s %c", p1.Cognome, p1.Telefono, &p1.M_F) != EOF)
  41.                 printf("%s %s %c\n", p1.Cognome, p1.Telefono, p1.M_F);
  42.             fclose(f);
  43.             break;
  44.         case 3:
  45.             if (!(f = fopen(NFile, "a")))
  46.             {
  47.                 printf("Errore!\n");
  48.                 exit(1);
  49.             }
  50.             getRubrica(&p1);
  51.             fprintf(f, "%s %s %c\n", p1.Cognome, p1.Telefono, p1.M_F);
  52.             fclose(f);
  53.         }
  54.     }
  55.     return 0;
  56. }
  57.  
  58. int menu(char *NFile)
  59. {
  60.     int scelta;
  61.     do {
  62.         printf("0 Esci\n");
  63.         printf("1 Apri File\n");
  64.         printf("2 Leggi File %s\n", NFile);
  65.         printf("3 Aggiungi al file %s\n", NFile);
  66.         printf("Scelta: ");
  67.         scanf("%d", &scelta);
  68.         if (scelta > 0 && scelta <= 3) return scelta;
  69.         else if (!scelta) exit(0);
  70.         printf("Valore non valido!\n");
  71.     } while (1);
  72.  
  73. }
  74. void getRubrica(Rubrica *p)
  75. {
  76.     while (getchar() != '\n');
  77.     printf("Cognome: ");
  78.     gets_s(p->Cognome);
  79.     printf("Telefono: ");
  80.     gets_s(p->Telefono);
  81.     printf("Sesso (M/F): ");
  82.     scanf("%c", &p->M_F);
  83.     return;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement