uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

files_ex1.c (1067B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 struct Student {
      6         char name[50];
      7         float grade;
      8         int id;
      9 };
     10 
     11 int
     12 main(int argc, char *argv[])
     13 {
     14         FILE *fp;
     15         struct Student *s;
     16 
     17         if ((s = malloc(sizeof(struct Student))) == NULL) {
     18                 fputs("malloc failed\n", stderr);
     19                 return 1;
     20         }
     21 
     22         if ((fp = fopen("data", "w")) == NULL) {
     23                 fputs("fopen failed\n", stderr);
     24                 return 1;
     25         }
     26 
     27         for (;;) {
     28                 printf("Name: ");
     29                 fgets(s->name, sizeof(s->name), stdin);
     30                 s->name[strlen(s->name) - 1] = '\0';
     31                 printf("ID: ");
     32                 scanf("%d", &s->id);
     33                 getchar();
     34                 printf("Grade: ");
     35                 scanf("%f", &s->grade);
     36                 getchar();
     37 
     38                 if (s->id < 0)
     39                         break;
     40                 fprintf(fp, "%d %s %.2f\n", s->id, s->name, s->grade);
     41         }
     42 
     43         fclose(fp);
     44         free(s);
     45 
     46         return 0;
     47 }