[text] Student Management System with File Handling (WIP)

Viewer

copydownloadembedprintName: Student Management System with File Handling (WIP)
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>   
  4. #include <time.h>
  5. #define SIZE 100
  6. #define true 0
  7. #define false 1
  8.  
  9. struct student
  10.         {
  11.                 char stdid[SIZE];
  12.                 char name[SIZE];
  13.         int  lvl;
  14.                 double sum, eng, math, sci, his, fil, genav;
  15.                
  16.         };
  17.        
  18. struct student st[SIZE];
  19. int count = 0; 
  20. FILE *f;
  21.  
  22. int writefile()
  23. {
  24.     int i;
  25.     f = fopen("sms.txt", "w");  
  26.     if (f == NULL)
  27.     return -1;                    
  28.     fprintf(f, "%d\n", count);
  29.     for (i = 0; i < count; ++i) 
  30.     {
  31.         fputs(st[i].stdid, f);
  32.         fprintf(f, "\n");
  33.         fputs(st[i].name, f);
  34.         fprintf(f, "\n");
  35.         fprintf(f, "%d\n", st[i].lvl);
  36.         fprintf(f, "\n");
  37.         fprintf(f, "%.2lf\n", st[i].eng);
  38.         fprintf(f, "%.2lf\n", st[i].math);
  39.         fprintf(f, "%.2lf\n", st[i].sci);
  40.         fprintf(f, "%.2lf\n", st[i].his);
  41.         fprintf(f, "%.2lf\n", st[i].fil);
  42.     }
  43.     fclose(f);
  44.     return 0;
  45. }
  46.  
  47. int writefile2()
  48. {
  49.     int i;
  50.     f = fopen("sms.txt", "w");  
  51.     if (f == NULL)
  52.     return -1;                    
  53.     fprintf(f, "%d\n", count);
  54.     for (i = 0; i < count; ++i) 
  55.     {
  56.         fputs(st[i].stdid, f);
  57.         fprintf(f, "\n");
  58.         fputs(st[i].name, f);
  59.         fprintf(f, "\n");
  60.         fprintf(f, "%d\n", st[i].lvl);
  61.         fprintf(f, "\n");
  62.     }
  63.     fclose(f);
  64.     return 0;
  65. }
  66.  
  67. int readFile() 
  68. {
  69.     int n = 0;
  70.     int i;
  71.     f = fopen("sms.txt", "r");
  72.     if (f == NULL)
  73.         return -1;
  74.     fscanf(f, "%d\n", &n);
  75.     for (i = 0; i < n; ++i)
  76.     {
  77.         fgets(st[i].stdid, SIZE, f);
  78.         st[i].stdid[strlen(st[i].stdid) - 1] = 0; // remove new lines
  79.         fgets(st[i].name, SIZE, f);
  80.         st[i].name[strlen(st[i].name)-1] = 0;
  81.         fscanf(f, "%d", &st[i].lvl);
  82.         fscanf(f, "%.2lf", &st[i].eng);
  83.         fscanf(f, "%.2lf", &st[i].math);
  84.         fscanf(f, "%.2lf", &st[i].sci);
  85.         fscanf(f, "%.2lf", &st[i].his);
  86.         fscanf(f, "%.2lf\n", &st[i].fil);
  87.     }
  88.     fclose(f);
  89.     return n;
  90. }
  91.  
  92. int readFile2() 
  93. {
  94.     int n = 0;
  95.     int i;
  96.     f = fopen("sms.txt", "r");
  97.     if (f == NULL)
  98.         return -1;
  99.     fscanf(f, "%d\n", &n);
  100.     for (i = 0; i < n; ++i)
  101.     {
  102.         fgets(st[i].stdid, SIZE, f);
  103.         st[i].stdid[strlen(st[i].stdid) - 1] = 0; // remove new lines
  104.         fgets(st[i].name, SIZE, f);
  105.         st[i].name[strlen(st[i].name)-1] = 0;
  106.         fscanf(f, "%d", &st[i].lvl);
  107.     fclose(f);
  108.     return n;
  109.         }
  110. }
  111.  
  112. void delay(int number_of_seconds)
  113. {
  114.     int milli_seconds = 1000 * number_of_seconds;
  115.     clock_t start_time = clock();
  116.     while (clock() < start_time + milli_seconds);
  117. }
  118.  
  119. void deletestudent()
  120.         count=readFile();
  121.         char stdid[SIZE]; 
  122.         int i,j;
  123.         int z=false;
  124.         printf("ENTER ID YOU WANT TO DELETE: ");
  125.         fflush(stdin);
  126.         gets(stdid);
  127.  
  128.         for(i=0;i<count;i++)
  129.         {
  130.                 z=true;
  131.                 if(strcmp(st[i].stdid,stdid)==0)
  132.                         { 
  133.                                 for( j=i; j<(count-1); j++)
  134.                                 {
  135.                                         st[j]=st[j+1];
  136.                                 }
  137.                                 printf("\nSTUDENT HAS BEEN REMOVED SUCCESSFULLY!");
  138.                                 count--;
  139.                         }
  140.         }
  141.                 if(z==false)
  142.                 {
  143.                         printf("STUDENT ID NOT FOUND!");
  144.                 }
  145.         writefile();
  146. }
  147.  
  148. int IDChecker (int i, int j)
  149. {
  150.         count=readFile();
  151.     printf("\nSTUDENT ID: ");
  152.     fflush(stdin);
  153.         gets(st[count].stdid);
  154.     if (strcmp(st[i].stdid,st[j].stdid)==0)
  155.         {
  156.         printf("ID NUMBER IS ALREADY TAKEN!");
  157.         return IDChecker(i++,j--);
  158.         }
  159. }
  160.  
  161. void addnewstudent()
  162. {
  163.         readFile2();
  164.       if (count>0) 
  165.                 {
  166.                   count=readFile2();
  167.                       IDChecker(0,count);
  168.                 }
  169.         else
  170.                 {
  171.                         printf("\nSTUDENT ID: ");
  172.                         fflush(stdin); 
  173.                         gets(st[count].stdid);
  174.                 }
  175.                 printf("FULL NAME: ");
  176.                 gets(st[count].name);
  177.                 {
  178.                         int c=10;
  179.                         int lvl;
  180.                         while (c!=0)
  181.                         {
  182.                                 printf("GRADE LEVEL(1-6): ");
  183.                                 scanf("%d", &(st[count].lvl));
  184.                                                                                                                        
  185.                                 if ((st[count].lvl)>=1 && (st[count].lvl)<=6)
  186.                                         {
  187.                                                 c=0;
  188.                                         }
  189.                                 else
  190.                                         {
  191.                                                 printf("\nSORRY, YOU HAVE ENTERED AN INVALID NUMBER. PLEASE TRY AGAIN.\n");
  192.                                         }
  193.                                 printf("\n");
  194.                         }
  195.                 }      
  196.                 ++count; 
  197.         writefile2();
  198. }
  199.  
  200. int checkID(char stdid[])
  201.         int i;
  202.         count=readFile();
  203.         readFile();
  204.       
  205.         for(i=0;i<count;i++)
  206.         {
  207.                  
  208.         if(strcmp(stdid,st[i].stdid)!=0)
  209.                 {
  210.                         fclose(f);
  211.                 }
  212.      return 1;
  213.       }
  214.        
  215.         fclose(f);
  216.        return 0;
  217. }
  218.  
  219. void displayallstudents()
  220. {
  221.         int i;
  222.  
  223.       count = readFile2();
  224.      
  225.                 printf("\n");
  226.             printf("\n");
  227.                 printf("\n\t\t\t\t   STUDENT NAME\t-\tSTUDENT ID\t-\tGRADE LEVEL");
  228.                 printf("\n\t\t\t    ----------------------------------------------------------------------\n");
  229.                 printf("\n");
  230.                 printf("\n");
  231.  
  232.      for (i=0;i<count;i++)
  233.         {
  234.      printf("\t\t\t\t%s\t-\t%s\t-\t%d\n", st[i].name, st[i].stdid, st[i].lvl);
  235.         }
  236. }
  237.  
  238. void inputgrades()
  239. {
  240.         char stdid[SIZE];
  241.         int test;
  242.         int i;
  243.         int choice;
  244.       printf("ENTER STUDENT ID: ");
  245.         fflush(stdin);
  246.         gets(stdid);
  247.         test=checkID(stdid);
  248.     if (test == 0)
  249.         {
  250.               printf("STUDENT ID NOT FOUND");
  251.        }
  252.        else
  253.        {
  254.                readFile();
  255.               {
  256.                         for(i=0;i<count;i++)
  257.                         {
  258.                                 if(strcmp(stdid,st[i].stdid)!=0)
  259.                                 writefile();
  260.                      else
  261.                              {
  262.                                      printf("\nFULL NAME: %s", st[i].name);
  263.                                         printf("\nSTUDENT ID: %s", st[i].stdid);
  264.                                         printf("\nGRADE LEVEL: %d", st[i].lvl);
  265.                                      printf("\n\nREPORT CARD:\n\n");
  266.                                         printf("ENGLISH:\t");
  267.                                     scanf("%lf", &(st[i].eng));
  268.                                     printf("MATH:\t\t");
  269.                                     scanf("%lf", &(st[i].math));
  270.                                     printf("SCIENCE:\t");
  271.                                     scanf("%lf", &(st[i].sci));
  272.                                     printf("HISTORY:\t");
  273.                                     scanf("%lf", &(st[i].his));
  274.                                     printf("FILIPINO:\t");
  275.                                     scanf("%lf", &(st[i].fil));
  276.                                 }
  277.                                 writefile();
  278.                         }
  279.                 }
  280.         }
  281.                 fclose(f); 
  282.                 f = fopen("sms.txt", "r");
  283.                 readFile();
  284.                 {
  285.                         writefile();
  286.                 }
  287.                 fclose(f);
  288. }
  289.  
  290. void viewgrades()
  291. {
  292.         char stdid[SIZE];
  293.         int test;
  294.         int i;
  295.         int choice;
  296.       printf("ENTER STUDENT ID: ");
  297.         fflush(stdin);
  298.         gets(stdid);
  299.         test=checkID(stdid);
  300.     if (test == 0)
  301.         {
  302.               printf("STUDENT ID NOT FOUND");
  303.        }
  304.        else
  305.        {
  306.                readFile();
  307.               {
  308.                         for(i=0;i<count;i++)
  309.                         {
  310.                                 if(strcmp(stdid,st[i].stdid)!=0)
  311.                                 writefile();
  312.                      else
  313.                              {
  314.                                      printf("\nWELCOME TO %s's PROFILE!\n", st[i].name);
  315.                                         printf("\nFULL NAME: %s", st[i].name);
  316.                                         printf("\nSTUDENT ID: %s", st[i].stdid);
  317.                                         printf("\nGRADE LEVEL: %d", st[i].lvl);
  318.                                         printf("\n\nREPORT CARD:\n\n");
  319.                                         printf("SUBJECT\t\t|\tGRADE\t\t|\tREMARKS");
  320.                                         printf("\n----------------------------------------------------------------------\n");
  321.                                         printf("\nENGLISH\t\t|\t%.2lf\t\t|\t", st[i].eng);
  322.                         if (st[i].eng < 75)
  323.                                 {
  324.                                         printf("FAILED\n");
  325.                                 }
  326.                         else
  327.                                 {
  328.                                         printf("PASSED\n");
  329.                                 }
  330.                 printf("MATH\t\t|\t%.2lf\t\t|\t", st[i].math);
  331.                         if (st[i].math < 75)
  332.                                 {
  333.                                         printf("FAILED\n");
  334.                                 }
  335.                         else
  336.                                 {
  337.                                         printf("PASSED\n");
  338.                                 }
  339.                 printf("SCIENCE\t\t|\t%.2lf\t\t|\t", st[i].sci);
  340.                         if (st[i].sci < 75)
  341.                                 {
  342.                                         printf("FAILED\n");
  343.                                 }
  344.                         else
  345.                                 {
  346.                                         printf("PASSED\n");
  347.                                 }
  348.                 printf("HISTORY\t\t|\t%.2lf\t\t|\t", st[i].his);
  349.                         if (st[i].his < 75)
  350.                                 {
  351.                                         printf("FAILED\n");
  352.                                 }
  353.                         else
  354.                                 {
  355.                                         printf("PASSED\n");
  356.                                 }
  357.                 printf("FILIPINO\t|\t%.2lf\t\t|\t", st[i].fil);
  358.                         if (st[i].fil < 75)
  359.                                 {
  360.                                         printf("FAILED\n");
  361.                                 }
  362.                         else
  363.                                 {
  364.                                         printf("PASSED\n");
  365.                                 }
  366.                 printf("----------------------------------------------------------------------\n");
  367.                 st[i].sum = st[i].eng + st[i].math + st[i].sci + st[i].his + st[i].fil;
  368.                 st[i].genav = st[i].sum / 5;
  369.                 printf("GENERAL AVERAGE\t|\t%.2lf\t\t|\t", st[i].genav);
  370.                         if (st[i].genav < 75)
  371.                                 {
  372.                                         printf("FAILED\n");
  373.                                 }
  374.                         else
  375.                                 {
  376.                                         printf("PASSED\n");
  377.                                 }
  378.         writefile();
  379.                                 }
  380.                                    
  381.                         }
  382.                 }
  383.                 fclose(f); 
  384.                 f = fopen("sms.txt", "r");
  385.                 readFile();
  386.                 {
  387.                         writefile();
  388.                 }
  389.                 fclose(f);
  390.         }
  391. }
  392. void editstudent()
  393. {      
  394.         char stdid[SIZE];
  395.         int test;
  396.         int i;
  397.         int choice;
  398.       printf("ENTER STUDENT ID: ");
  399.         fflush(stdin);
  400.         gets(stdid);
  401.         test=checkID(stdid);
  402.     if (test == 0)
  403.         {
  404.               printf("STUDENT ID NOT FOUND");
  405.        }
  406.        else
  407.        {
  408.                readFile();
  409.               {
  410.                         for(i=0;i<count;i++)
  411.                         {
  412.                                 if(strcmp(stdid,st[i].stdid)!=0)
  413.                                 writefile();
  414.                      else
  415.                              {
  416.                                      printf("\nWHAT DO YOU WANT TO UPDATE?\n\n");
  417.                             printf("<1> STUDENT ID\n");
  418.                                 printf("<2> FULL NAME\n");
  419.                             printf("<3> GRADE LEVEL\n");
  420.                             printf("<4> ENGLISH GRADE\n");
  421.                             printf("<5> MATH GRADE\n");
  422.                             printf("<6> SCIENCE GRADE\n");
  423.                             printf("<7> HISTORY GRADE\n");
  424.                             printf("<8> FILIPINO GRADE\n");
  425.                             printf("<9> UPDATE ALL INFORMATION OF STUDENT\n");
  426.                             printf("\nCHOOSE TASK TO PERFORM: ");
  427.                                     fflush(stdin);
  428.                                     scanf("%d", &choice);
  429.                                
  430.                                     switch (choice)
  431.                                     {
  432.                                     case 1:
  433.                                             printf("STUDENT ID: ");
  434.                                                fflush(stdin);
  435.                                         gets(st[i].stdid);
  436.                                     break;
  437.                                     case 2:
  438.                                             printf("FULL NAME: ");
  439.                                             fflush(stdin);
  440.                                         gets(st[i].name);
  441.                                     break;
  442.                                     case 3:
  443.                                             {
  444.                                                         int c=10;
  445.                                                         int lvl;
  446.                                                         while (c!=0)
  447.                                                         {
  448.                                                                 printf("\nGRADE LEVEL(1-6): ");
  449.                                                                 scanf("%d", &(st[i].lvl));
  450.                                                                                                                                                        
  451.                                                                 if ((st[i].lvl)>=1 && (st[i].lvl)<=6)
  452.                                                                         {
  453.                                                                                 c=0;
  454.                                                                         }
  455.                                                                 else
  456.                                                                         {
  457.                                                                                 printf("\nSORRY, YOU HAVE ENTERED AN INVALID NUMBER. PLEASE TRY AGAIN.\n");
  458.                                                                         }
  459.                                                                 printf("\n");
  460.                                                         }
  461.                                                 }      
  462.                                     break;
  463.                                     case 4:
  464.                                             printf("\nENGLISH GRADE: ");
  465.                                                 scanf("%lf", &(st[i].eng));
  466.                                     break;
  467.                                     case 5:
  468.                                            printf("\nMATH GRADE: ");
  469.                                                 scanf("%lf", &(st[i].math));
  470.                                         break;
  471.                                         case 6:
  472.                                            printf("\nSCIENCE GRADE: ");
  473.                                                 scanf("%lf", &(st[i].sci));
  474.                                         break;
  475.                                         case 7:
  476.                                            printf("\nHISTORY GRADE: ");
  477.                                                 scanf("%lf", &(st[i].his));
  478.                                         break;
  479.                                         case 8:
  480.                                            printf("\nFILIPINO GRADE: ");
  481.                                                 scanf("%lf", &(st[i].fil));
  482.                                         break;
  483.                                     default:
  484.                                            printf("YOU HAVE ENTERED AN INVALID NUMBER.");
  485.                                     break;
  486.                                     }
  487.                                    writefile();
  488.                                 }
  489.                                    
  490.                         }
  491.                 }
  492.                 fclose(f); 
  493.                 f = fopen("sms.txt", "r");
  494.                 readFile();
  495.                 {
  496.                         writefile();
  497.                 }
  498.                 fclose(f);
  499.                 printf("PROFILE UPDATED");
  500.         }
  501. }
  502.  
  503.  
  504.  
  505.  
  506. int main ()
  507. {
  508.         int a=10;
  509.         int id;
  510.         char name[SIZE];
  511.         char pswd[SIZE];
  512.        
  513.         system("color F6");
  514.         printf("\n");
  515.         printf("\n");
  516.         printf("\n");
  517.     printf("\t\t            O==========================================================O\n");
  518.     printf("\t\t            |  OUR LADY OF FATIMA UNIVERISTY - VALENZUELA CITY CAMPUS  |\n");
  519.     printf("\t\t            |                                                          |");
  520.     printf("\n\t\t          |           WELCOME TO THE INSTRUCTOR PORTAL!            |\n");       
  521.     printf("\t\t            O==========================================================O\n");
  522.         printf("\n");
  523.         printf("\n");
  524.         printf("\n");
  525.        
  526.        
  527.         while (a!=0)
  528.         {
  529.                 char un[SIZE];
  530.                 char pswd[SIZE];
  531.                 printf("\n\t\t\t\t\t\t   USERNAME: ");
  532.                 scanf("%s", un);
  533.                 printf("\t\t\t\t\t\t   PASSWORD: ");
  534.                 scanf("%s", pswd);
  535.  
  536.                 if (strcmp(un, "olfu") == 0 && strcmp(pswd, "ccs") == 0)
  537.                 {
  538.                         printf("\n\t\t\t\t\tLOGIN SUCCESSFUL! PLEASE WAIT A MOMENT...");
  539.                         a=0;
  540.                         delay(2);
  541.                         system("cls");
  542.                 }
  543.                 else
  544.                 {
  545.                         printf("\n\t\t\t  THE USERNAME AND/OR PASSWORD IS INCORRECT. PLEASE TRY AGAIN IN 3 SECONDS.");
  546.                         delay(3);
  547.                         system("cls");
  548.                 }
  549.                 printf("\n");
  550.         }
  551.        
  552.         int i, j, ch, num, flag, stdid;
  553.         int choice, upd, updchoice;
  554.         struct student *st;
  555.         num = 0;
  556.         
  557.                 st = (struct student *)malloc(sizeof(struct student) * num);
  558.     
  559.                 while (1) 
  560.                 {
  561.                                 printf("\n");
  562.                                 printf("\n");
  563.                         printf("\n\t\t          O==================STUDENT MANAGEMENT SYSTEM==================O");
  564.                         printf("\n\t\t\t\t|                                                           |");
  565.                                 printf("\n\t\t\t\t|             <1> ADD A NEW STUDENT\t\t\t      |\n");
  566.                                 printf("\t\t\t\t|               <2> SHOW STUDENT LIST\t\t\t      |\n");
  567.                                 printf("\t\t\t\t|               <3> INPUT STUDENT'S GRADES\t\t      |\n");
  568.                                 printf("\t\t\t\t|               <4> VIEW STUDENT'S GRADES\t\t      |\n");
  569.                                 printf("\t\t\t\t|               <5> EDIT STUDENT PROFILE\t\t      |\n");
  570.                                 printf("\t\t\t\t|               <6> DELETE A STUDENT\t\t\t      |\n");
  571.                                 printf("\t\t\t\t|               <7> EXIT\t\t\t\t      |");
  572.                                 printf("\n\t\t\t\t|                                                           |");
  573.                                 printf("\n\t\t          O=============================================================O\n");
  574.                                 printf("\n");
  575.                                 printf("\n");
  576.                                 printf("\n\t\t\t\t\t      CHOOSE A TASK TO PERFORM (1-7): ");
  577.                                
  578.                                 scanf("%d", &choice);
  579.                                     switch(choice)
  580.                                         {
  581.                                 case 1: addnewstudent();
  582.                                         break;
  583.                                 case 2: displayallstudents();
  584.                                                         break;
  585.                                 case 3: inputgrades();
  586.                                           break;
  587.                                 case 4: viewgrades();
  588.                                         break;
  589.                                  case 5:        editstudent();
  590.                                                        break;
  591.                                      case 6:        deletestudent();
  592.                                                        break;
  593.                                         case 7:        exit(1);
  594.                                                      break;
  595.                                         default:
  596.                                     printf("YOU HAVE ENTERED AN INVALID VALUE");
  597.                                     break;
  598.                               }
  599.   }
  600.   while(choice!=8);
  601. }

Editor

You can edit this paste and save as new:


File Description
  • Student Management System with File Handling (WIP)
  • Paste Code
  • 02 Dec-2022
  • 13.01 Kb
You can Share it: