Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- int ID;
- char Nome[25];
- }Studente;
- void Leggi(char *nomefile);
- void Inserisci(char *nomefile);
- void Cancella(char *nomefile);
- int main()
- {
- int i;
- for(i=0;i<2;i++)
- Inserisci("Informazioni.dat");
- printf("\n");
- Leggi("Informazioni.dat");
- Cancella("Informazioni.dat");
- printf("\n");
- Leggi("Informazioni.dat");
- return 0;
- }
- void Inserisci(char *nomefile){
- FILE *pf;
- Studente *s;
- s =(Studente*)malloc(sizeof(Studente));
- if(s==NULL){
- printf("Impossibile allocare memoria...");
- exit (1);
- }
- pf = fopen(nomefile,"a+b");
- if(pf){
- printf("\nInserisci ID : ");
- scanf("%d",&s->ID);
- getchar();
- printf("Inserisci il nome : ");
- gets(s->Nome);
- fwrite(s,sizeof( Studente),1,pf);
- fclose(pf);
- free(s);
- }
- }
- void Leggi(char *nomefile){
- FILE *pf;
- Studente s;
- pf = fopen(nomefile,"r");
- if(pf){
- while(fread(&s,sizeof(Studente),1,pf)!=0){
- printf("ID: %d\n",s.ID);
- printf("Nome: %s\n",s.Nome);
- printf("\n");
- }
- fclose(pf);
- }
- else{
- printf("Erroe durante l'apertuta del file...");
- exit(1);
- }
- }
- void Cancella(char *nomefile){
- FILE *pf;
- int ID_Studente;
- Studente s;
- int trovato=0;
- pf = fopen(nomefile,"r+b");
- printf("Inserisci ID da cancellare :");
- scanf("%d",&ID_Studente);
- while (!feof(pf) && trovato==0) // scorro tutto il file finchè non lo trovo
- {
- fread(&s,sizeof(Studente),1,pf);
- if(s.ID == ID_Studente)
- {
- printf("\nTrovato...");
- trovato=1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement