c - how to print values from file after appending -


i have following problem: must must make student file name, age , grades. works ok, problem when printing on screen. let's read 3 students, when type 'l' printing content on screen works great. when press 'a' add student file(append), student appear in file(so have total of 4 students), when press 'l' print again, prints 3 out of 4 students, including appended one, removes anther one. 's' stands write file.

#include <stdio.h> #include <stdlib.h> #include <ctype.h>  struct student{     char nume[20];     int varsta;     float medie; };  void citire_date(struct student *studenti, file *f, int n){     int i;      for(i=0;i<n;i++){         printf("introduceti datele studentului %d\n", i+1);         printf("nume: "); fflush(stdin); gets((studenti+i)->nume);         printf("varsta: "); scanf("%d", &((studenti+i)->varsta));         printf("medie: "); scanf("%f", &((studenti+i)->medie));         fprintf(f, "nume: %s\n", (studenti+i)->nume);         fprintf(f, "varsta: %d\n", (studenti+i)->varsta);         fprintf(f, "medie: %.2f\n", (studenti+i)->medie);         fprintf(f, "\n");     } }  void afisare_date(struct student *studenti, int n){     int i;     char nume1;     int varsta1;     float medie1;      for(i=0;i<n;i++){        printf("nume: %s\n", (studenti+i)->nume);         printf("varsta: %d\n", (studenti+i)->varsta);         printf("medie: %.2f\n", (studenti+i)->medie);         printf("\n");     } }  void append(struct student *studenti, file *f){     int i;      for(i=0;i<1;i++){         printf("nume: "); fflush(stdin); gets((studenti+i)->nume);         printf("varsta: "); scanf("%d", &((studenti+i)->varsta));         printf("medie: "); scanf("%f", &((studenti+i)->medie));         fprintf(f, "nume: %s\n", (studenti+i)->nume);         fprintf(f, "varsta: %d\n", (studenti+i)->varsta);         fprintf(f, "medie: %.2f\n", (studenti+i)->medie);         fprintf(f, "\n");     } }  int main() {     file *f;     struct student studenti[20];     int n;     char optiune;      f=fopen("studenti.txt", "w");     if(f==null){         printf("nu s-a putut deschide/crea fisierul pentru scriere.\n");         exit(1);     }      printf("\n");      do{         printf("s. scriere fisier.\n");         printf("a. adaugarea unui nou student in fisier.\n");         printf("l. listarea datelor tuturor studentilor.\n");         printf("m. modificarea datelor unui student.\n");         printf("c. cautarea unui student dupa nume.\n");         printf("x. terminarea programului.\n");         printf("introduceti optiunea: "); fflush(stdin); optiune=toupper(getchar());         printf("\n");           switch(optiune){             case 's':                 printf("introduceti numarul de studenti: "); scanf("%d", &n);                 citire_date(studenti, f, n);                 fclose(f);                 printf("\n");                 break;             case 'a':                 f = fopen("studenti.txt", "a");                 if(f==null ){                     printf("nu s-a putut deschide fisierul pentru adaugare.\n");                     exit(2);                 }                 append(studenti, f);                 fclose(f);                 break;             case 'l':                 afisare_date(studenti, n);                 break;             case 'm':                 break;             case 'c':                 break;             case 'x':                 break;             default:                 printf("\nati introdus o optiune eronata!\n");                 break;          }     }while(optiune!='x');          return 0; } 

i think need use fscanf, not know how becasue read file "nume: name" "varsta: 19" , on. thank in advance.

i edited main, should clear see now.

int main() {     file *f;     struct student studenti[20];     int n;     char optiune;       printf("introduceti numarul de studenti: "); scanf("%d", &n);      f=fopen("studenti.txt", "w");     if(f==null){         printf("nu s-a putut deschide/crea fisierul pentru scriere.\n");         exit(1);     }     citire_date(studenti, f, n);     fclose(f);      printf("\n");      do{         printf("a. adaugarea unui nou student in fisier.\n");         printf("l. listarea datelor tuturor studentilor.\n");         printf("m. modificarea datelor unui student.\n");         printf("c. cautarea unui student dupa nume.\n");         printf("x. terminarea programului.\n");         printf("introduceti optiunea: "); fflush(stdin); optiune=toupper(getchar());         printf("\n");           switch(optiune){             case 'a':                 f = fopen("studenti.txt", "a");                 if(f==null ){                     printf("nu s-a putut deschide fisierul pentru adaugare.\n");                     exit(2);                 }                 append(studenti, f);                 fclose(f);                 break;             case 'l':                 afisare_date(studenti, n);                 break;             case 'm':                 break;             case 'c':                 break;             case 'x':                 break;             default:                 printf("\nati introdus o optiune eronata!\n");                 break;          }     }while(optiune!='x');          return 0; } 

any suggestions ?


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -