training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

mkrecord.c (1609B)


      1 /* Exercise 11.8 */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 #define SIZE 29
      7 
      8 struct record {
      9    int account;
     10    char name[SIZE];
     11    char address[SIZE];
     12    int phone;
     13    double balance; 
     14    char notes[255];
     15 };
     16 
     17 int main(void)
     18 {
     19    FILE *master, *trans;
     20    struct record rec;
     21 
     22    if( (master = fopen("oldmast.dat", "w")) == NULL ) {
     23       printf("Shit, i can't open the file %s\n", "oldmast.dat");
     24       exit(-1);
     25    }
     26 
     27    if( (trans = fopen("trans.dat", "w")) == NULL ) {
     28       printf("Oh, fucked filesystem.. i can't open the file %s\n", "trans.dat");
     29       exit(-1);
     30    }
     31 
     32    printf("Inserting data for the master file\n");
     33 
     34    printf("Account number (0 to end): ");
     35    scanf("%d", &rec.account);
     36 
     37    /* writing to the "master" file */
     38    while( rec.account > 0 ) {
     39       printf("Name: ");
     40       scanf("%s", rec.name);
     41       printf("Balance: ");
     42       scanf("%lf", &rec.balance);
     43 
     44       fwrite(&rec, sizeof(struct record), 1, master);
     45       printf("record stored!\n\n");
     46 
     47       printf("Account number (0 to end): ");
     48       scanf("%d", &rec.account);
     49    }
     50 
     51    printf("Inserting data for the transactions file\n");
     52 
     53    printf("Avvount number (0 to end): ");
     54    scanf("%d", &rec.account);
     55 
     56    /* writing to the "trans" file */
     57    while( rec.account > 0 ) {
     58 
     59       rec.name[0] = '\0'; /* not required */ 
     60 
     61       printf("Balance: ");
     62       scanf("%lf", &rec.balance);
     63 
     64       fwrite(&rec, sizeof(struct record), 1, trans);
     65       printf("record stored!\n\n");
     66 
     67       printf("Account number (0 to end): ");
     68       scanf("%d", &rec.account);
     69    }
     70 
     71    fclose(master);
     72    fclose(trans);
     73 
     74    return 0;
     75 } /* E0F main */
     76