bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc093.c

luc093.c (3365B)


      1 /* Update Master file (Roll, Name) using Transaction file (Roll, Code Add/Delete). Both files are sorted by Roll.
      2 */
      3 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(i) */
      4 
      5 /* This file is auto-generated by a bot. */
      6 /* This code is not compiled; it is for reference only. */
      7 
      8 
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <ctype.h>
     13 
     14 struct student {
     15     int roll;
     16     char name[30];
     17 };
     18 
     19 struct trans {
     20     int roll;
     21     char code; // 'A' for Add, 'D' for Delete
     22     char name[30]; // Only needed for Add
     23 };
     24 
     25 void create_files();
     26 
     27 int main()
     28 {
     29     FILE *fm, *ft, *fn;
     30     struct student m;
     31     struct trans t;
     32     int has_m, has_t;
     33 
     34     create_files();
     35 
     36     fm = fopen("MASTER.DAT", "rb");
     37     ft = fopen("TRANS_ROLL.DAT", "rb");
     38     fn = fopen("NEW_MASTER.DAT", "wb");
     39 
     40     if (!fm || !ft || !fn) exit(1);
     41 
     42     has_m = fread(&m, sizeof(struct student), 1, fm);
     43     has_t = fread(&t, sizeof(struct trans), 1, ft);
     44 
     45     printf("Merging Master and Transaction files...\n");
     46 
     47     while (has_m && has_t)
     48     {
     49         if (m.roll < t.roll)
     50         {
     51             // No transaction for this master record, keep it
     52             fwrite(&m, sizeof(struct student), 1, fn);
     53             has_m = fread(&m, sizeof(struct student), 1, fm);
     54         }
     55         else if (m.roll == t.roll)
     56         {
     57             // Transaction affects this record
     58             if (t.code == 'D')
     59             {
     60                 printf("Deleting Roll %d\n", m.roll);
     61                 // Skip writing 'm' to file (Deleting)
     62                 has_m = fread(&m, sizeof(struct student), 1, fm);
     63             }
     64             else
     65             {
     66                 // Logic error: Can't ADD if ID exists. Maybe Update?
     67                 // Assuming replace or skip. Let's skip T and keep M for now.
     68                 has_m = fread(&m, sizeof(struct student), 1, fm);
     69             }
     70             has_t = fread(&t, sizeof(struct trans), 1, ft);
     71         }
     72         else // m.roll > t.roll
     73         {
     74             // Transaction ID is new
     75             if (t.code == 'A')
     76             {
     77                 struct student new_s;
     78                 new_s.roll = t.roll;
     79                 strcpy(new_s.name, t.name);
     80                 fwrite(&new_s, sizeof(struct student), 1, fn);
     81                 printf("Adding Roll %d\n", new_s.roll);
     82             }
     83             has_t = fread(&t, sizeof(struct trans), 1, ft);
     84         }
     85     }
     86 
     87     // Process remaining Master
     88     while (has_m)
     89     {
     90         fwrite(&m, sizeof(struct student), 1, fn);
     91         has_m = fread(&m, sizeof(struct student), 1, fm);
     92     }
     93 
     94     // Process remaining Transactions (Only Adds)
     95     while (has_t)
     96     {
     97         if (t.code == 'A')
     98         {
     99             struct student new_s = {t.roll, ""};
    100             strcpy(new_s.name, t.name);
    101             fwrite(&new_s, sizeof(struct student), 1, fn);
    102             printf("Adding Roll %d\n", new_s.roll);
    103         }
    104         has_t = fread(&t, sizeof(struct trans), 1, ft);
    105     }
    106 
    107     fclose(fm); fclose(ft); fclose(fn);
    108     return 0;
    109 }
    110 
    111 void create_files()
    112 {
    113     struct student m[] = {{10, "A"}, {20, "B"}, {30, "C"}};
    114     struct trans t[] = {{15, 'A', "NewGuy"}, {20, 'D', ""}, {40, 'A', "LastGuy"}};
    115     
    116     FILE *f1 = fopen("MASTER.DAT", "wb");
    117     fwrite(m, sizeof(struct student), 3, f1);
    118     fclose(f1);
    119 
    120     FILE *f2 = fopen("TRANS_ROLL.DAT", "wb");
    121     fwrite(t, sizeof(struct trans), 3, f2);
    122     fclose(f2);
    123 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror