Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rubrica.cpp : definisce il punto di ingresso dell'applicazione console.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- char Cognome[30];
- char Telefono[30];
- char M_F;
- }Rubrica;
- int menu(char *NFile);
- void getRubrica(Rubrica *p);
- int main()
- {
- FILE *f;
- char NFile[30] = { "\0" };
- while (1)
- {
- Rubrica p1;
- switch (menu(NFile))
- {
- case 1:
- printf("Nome File: ");
- scanf("%s", NFile);
- while (getchar() != '\n');
- if (!(f = fopen(NFile, "a+")))
- {
- printf("Errore!\n");
- exit(1);
- }
- break;
- case 2:
- if (!(f = fopen(NFile, "r")))
- {
- printf("Errore!\n");
- exit(1);
- }
- while (fscanf(f, "%s %s %c", p1.Cognome, p1.Telefono, &p1.M_F) != EOF)
- printf("%s %s %c\n", p1.Cognome, p1.Telefono, p1.M_F);
- fclose(f);
- break;
- case 3:
- if (!(f = fopen(NFile, "a")))
- {
- printf("Errore!\n");
- exit(1);
- }
- getRubrica(&p1);
- fprintf(f, "%s %s %c\n", p1.Cognome, p1.Telefono, p1.M_F);
- fclose(f);
- }
- }
- return 0;
- }
- int menu(char *NFile)
- {
- int scelta;
- do {
- printf("0 Esci\n");
- printf("1 Apri File\n");
- printf("2 Leggi File %s\n", NFile);
- printf("3 Aggiungi al file %s\n", NFile);
- printf("Scelta: ");
- scanf("%d", &scelta);
- if (scelta > 0 && scelta <= 3) return scelta;
- else if (!scelta) exit(0);
- printf("Valore non valido!\n");
- } while (1);
- }
- void getRubrica(Rubrica *p)
- {
- while (getchar() != '\n');
- printf("Cognome: ");
- gets_s(p->Cognome);
- printf("Telefono: ");
- gets_s(p->Telefono);
- printf("Sesso (M/F): ");
- scanf("%c", &p->M_F);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement