Originariamente inviata da
oregon
Carica tutto in memoria, fai le modifiche in memoria e a richiesta o alla fine salvi su disco.
Ho messo giu' un codice di prova , in pratica se provo a premere "1" per la scrittura sul file avviene correttamente, quando premo 2 invece non mi visualizza l output, questo posso vederlo solo dopo aver chiuso il programma e premendo 2 , se lo visualizzo prima non appare nulla, potresti aiutarmi? ecco il codice:
codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//struttura
struct person
{
int id;
char fname[20];
char lname[20];
};
int main ()
{
//******************************************************************************
unsigned int scelta; //scelta per il menu
char torna; //ritorno al menu o uscita
do {
system("COLOR E"); //modifica il colore delle scritte
printf("*************************\n"); //*
printf("BENVENUTO IN SPOTIFY\n"); //*
printf("*************************\n"); //*
//*
//menu riguardante solo artisti //*
printf("1) Scrivi su file gli artisti\n\n"); //*
printf("2) Visualizza gli artisti scritti su file\n\n"); //*
printf("3) Aggiungi/Modifica/Elimina--Artisti\n\n"); //*
//*
//menu riguardante solo utenti //*
printf("4) Scrivi su file gli utenti\n\n"); //*
printf("5) Visualizza gli utenti scritti su file\n\n"); //*
printf("6) Aggiungi/Modifica/Elimina--Utenti\n\n"); //*
//*
//preferenze utenti //*
printf("7) Scrivi su file le preferenze degli utenti\n\n"); //*
printf("8) Aggiungi/Modifica/Elimina--Preferenze\n\n"); //*
//*
//ricerca e ordinamento artisti //*
printf("9) Ricerca degli artisti\n\n"); //*
printf("10) Ordina gli artisti\n\n"); //*
//*
//visualizza utente //*
printf("11) Mostra il profilo completo di un singolo utente\n\n"); //*
//*
//scelta //*
menu: //*
printf("Inserisci numero scelto: "); //*
//*
scanf("%u",&scelta);
switch(scelta){
case 1:
{system("cls");
FILE *outfile;
// open file for writing
outfile = fopen ("person.dat", "w");
if (outfile == NULL)
{
fprintf(stderr, "\nError opend file\n");
exit (1);
}
struct person input1 = {1, "rohan", "sharma"};
struct person input2 = {2, "mahendra", "dhoni"};
// write struct to file
fwrite (&input1, sizeof(struct person), 1, outfile);
fwrite (&input2, sizeof(struct person), 1, outfile);
if(fwrite != 0)
printf("contents to file written successfully !\n");
else
printf("error writing file !\n");
system("pause");
system("cls");}
break;
case 2:
{system("cls");
FILE *infile;
struct person input;
// Open person.dat for reading
infile = fopen ("person.dat", "r");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
exit (1);
}
// read file contents till end of file
while(fread(&input, sizeof(struct person), 1, infile))
printf ("id = %d name = %s %s\n", input.id,
input.fname, input.lname);
system("pause");
system("cls");}
break;
case 3:
{system("cls");
//funzione1();
system("pause");
system("cls");}
break;
case 4:
{system("cls");
//funzione1();
system("pause");
system("cls");}
break;
case 5:
{system("cls");
//funzione1();
system("pause");
system("cls");}
break;
case 6:
{system("cls");
//funzione1();
system("pause");
system("cls");}
break;
case 7:
{system("cls");
//funzione1();
system("pause");
system("cls");}
break;
case 8:
{system("cls");
//funzione1();
system("pause");
system("cls");}
break;
case 9:
{system("cls");
//funzione1();
system("pause");
system("cls");}
break;
case 10:
{system("cls");
//funzione1();
system("pause");
system("cls");}
break;
case 11:
{system("cls");
//funzione1();
system("pause");
system("cls");}
break;
default:
system("cls");
printf("Scelta non valida\n");
}
printf("Si desidera tornare al Menu' Principale?\n\n");
printf("[y] Si, desidero tornare al Menu' Principale\n\n");
printf("[n] No, desidero uscire\n\n");
printf("Effettua la tua scelta: ");
scanf("%s",&torna);
system("cls");
}
while (torna!='n');
return 0;
}