bsc

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

root / semester_1 / letusc / luc090.c

luc090.c (2078B)


      1 /* Read employee records (code, name, date, salary), sort them by Date of Joining, and write to a target file.
      2 */
      3 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(f) */
      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 date { int d, m, y; };
     15 struct employee {
     16     int empcode[6]; // Not used as int array usually, likely int id. Assuming int code.
     17     char empname[20];
     18     struct date join_date;
     19     float salary;
     20 };
     21 
     22 // Redefining struct for easier usage assuming empcode is int
     23 struct emp_clean {
     24     int code;
     25     char name[20];
     26     struct date doj;
     27     float salary;
     28 };
     29 
     30 void create_emp_file();
     31 int compare_dates(const void *a, const void *b);
     32 
     33 int main()
     34 {
     35     FILE *fp, *ft;
     36     struct emp_clean e[50];
     37     int count = 0, i;
     38 
     39     create_emp_file();
     40 
     41     fp = fopen("employee.dat", "rb");
     42     if (!fp) return 1;
     43 
     44     while (fread(&e[count], sizeof(struct emp_clean), 1, fp) == 1)
     45         count++;
     46     fclose(fp);
     47 
     48     qsort(e, count, sizeof(struct emp_clean), compare_dates);
     49 
     50     ft = fopen("emp_sorted.dat", "wb");
     51     fwrite(e, sizeof(struct emp_clean), count, ft);
     52     fclose(ft);
     53 
     54     printf("Sorted records written to 'emp_sorted.dat'.\nDisplaying sorted list:\n");
     55     for(i=0; i<count; i++)
     56         printf("%s - %02d/%02d/%04d\n", e[i].name, e[i].doj.d, e[i].doj.m, e[i].doj.y);
     57 
     58     return 0;
     59 }
     60 
     61 int compare_dates(const void *a, const void *b)
     62 {
     63     struct emp_clean *e1 = (struct emp_clean *)a;
     64     struct emp_clean *e2 = (struct emp_clean *)b;
     65 
     66     if (e1->doj.y != e2->doj.y) return e1->doj.y - e2->doj.y;
     67     if (e1->doj.m != e2->doj.m) return e1->doj.m - e2->doj.m;
     68     return e1->doj.d - e2->doj.d;
     69 }
     70 
     71 void create_emp_file()
     72 {
     73     struct emp_clean data[] = {
     74         {1, "John", {12, 5, 2022}, 5000},
     75         {2, "Jane", {10, 1, 2020}, 6000}, // Senior
     76         {3, "Bob",  {15, 8, 2021}, 5500}
     77     };
     78     FILE *f = fopen("employee.dat", "wb");
     79     fwrite(data, sizeof(struct emp_clean), 3, f);
     80     fclose(f);
     81 }
© 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