Advertisement
Guest User

Untitled

a guest
May 21st, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.15 KB | None | 0 0
  1. /*Student Record System to insert student and parents information,record and remark,search,display,
  2.  delete and update student's and parent's record with remark*/
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. /*structure to read date*/
  7. struct date
  8. {
  9.     int dd;
  10.     //*day
  11.     int mm;
  12.     //*month
  13.     int yy;
  14.     //*year
  15. };
  16. /*structure to read address*/
  17. struct address
  18.     {
  19.  
  20.         char country[15];
  21.         char city[15];
  22.         char street_address[20];
  23.         char temp_address[20];
  24.         char perm_address[20];
  25.     };
  26. /*structure to read student information */
  27. struct student
  28. {
  29.     int roll;
  30.     char f_name[30];
  31.     char l_name[30];
  32.     char program[30];
  33.     char level[30];
  34.     struct address a;
  35.     struct date d_o_b;
  36.     char contact[11];
  37.     char e_mail[20];
  38.     char gender;
  39. }db_student;
  40. /*structure to read parents information */
  41. struct parents
  42. {
  43.     char f_name[30];
  44.     char l_name[30];
  45.     char relation[30];
  46.     struct address a;
  47.     char contact[10];
  48.     char e_mail[20];
  49. }db_parents;
  50. //function to add student record to
  51. //add student information to the file
  52. //"student_info_file.dat
  53. void student_info_write();
  54. //function prototype for student information
  55. void parents_info_write();
  56. //function prototype for parents information
  57. void print_student_info();
  58. //function prototype to print student information
  59. int student_available(int);
  60.  
  61. void student_info_write()
  62. {
  63.     FILE *fp;
  64.     fp=fopen("student_info_file.dat","a");
  65.     /*pointer=fopen("file_name","open mode");
  66.     a=append,add
  67.     */
  68.     fflush(stdin);
  69.     printf("\n enter first name:");
  70.     gets(db_student.f_name);
  71.     printf("\n enter last name:");
  72.     gets(db_student.l_name);
  73.     printf("\n enter program:");
  74.     gets(db_student.program);
  75.     printf("\n enter level:");
  76.     gets(db_student.level);
  77.     printf("\n enter country:");
  78.     gets(db_student.a.country);
  79.     printf("\n enter city:");
  80.     gets(db_student.a.city);
  81.     printf("\n enter street_address:");
  82.     gets(db_student.a.street_address);
  83.     printf("\n enter temp_address:");
  84.     gets(db_student.a.temp_address);
  85.     printf("\n enter perm_address:");
  86.     gets(db_student.a.perm_address);
  87.     printf("\n enter d.o.b:");
  88.     printf("\n enter year:");
  89.     scanf("%d",&db_student.d_o_b.yy);
  90.     printf("\n month:");
  91.     scanf("%d",&db_student.d_o_b.mm);
  92.     printf("\n day:");
  93.     scanf("%d",db_student.d_o_b.dd);
  94.     printf("\n enter contact:");
  95.     sacnf("%d",&db_student.contact);
  96.     printf("\n enter e_mail:");
  97.     gets(db_student.e_mail);
  98.     printf("\n gender?(M/F)");
  99.     gets(db_student.gender);
  100.     fwrite(&db_student,sizeof(db_student),1,fp);
  101.     fclose(fp);
  102. }
  103.  
  104. //function to write parents information
  105. void parents_info_write()
  106. {
  107.     FILE *fp;
  108.     fp=fopen("parents_info_file.dat","a");
  109.     fflush(stdin);
  110.     printf("\n enter first name:");
  111.     gets(db_parents.f_name);
  112.     fflush(stdin);
  113.     printf("\n enter last name:");
  114.     gets(db_parents.l_name);
  115.     fflush(stdin);
  116.     printf("\n enter relation:");
  117.     gets(db_parents.relation);
  118.     fflush(stdin);
  119.     printf("\n enter country:");
  120.     gets(db_parents.a.country);
  121.     fflush(stdin);
  122.     printf("\n enter city:");
  123.     gets(db_parents.a.city);
  124.     fflush(stdin);
  125.     printf("\n enter street_address:");
  126.     gets(db_parents.a.street_address);
  127.     fflush(stdin);
  128.     printf("\n enter temp_address:");
  129.     gets(db_parents.a.temp_address);
  130.     fflush(stdin);
  131.     printf("\n enter perm_address:");
  132.     gets(db_parents.a.perm_address);
  133.     fflush(stdin);
  134.     printf("\n enter contact:");
  135.     gets(db_parents.contact);
  136.     fflush(stdin);
  137.     printf("\n enter e_mail:");
  138.     gets(db_parents.e_mail);
  139.  
  140.     fwrite(&db_parents,sizeof(db_parents),1,fp);
  141.     fclose(fp);
  142. }
  143. /*
  144.  
  145. function read and print student info
  146. from the file "student_info_file.dat"
  147. and display the information
  148.  
  149. */
  150. void print_student_info()
  151. //print according to roll no
  152. {
  153.     FILE *fp;
  154.     int r,i;
  155.     printf("\n Enter roll number to search:");
  156.     scanf("%d",&r);
  157.     i=student_available(r);
  158.     if(i==0)
  159.     {
  160.     fp=fopen("student_info_file.dat","r");
  161.     while(!feof(fp))
  162.     {
  163.         if(db_student.roll==r);
  164.         {
  165.         printf("\n first name:");
  166.         printf("\n%s",db_student.f_name);
  167.         printf("\n last name:");
  168.         printf("\n program:");
  169.         printf("\n%s",db_student.program);
  170.         printf("\n level:");
  171.         printf("\n%s",db_student.level);
  172.         printf("\n country:");
  173.         printf("\n%s",db_student.a.country);
  174.         printf("\n city:");
  175.         printf("\n%s",db_student.a.city);
  176.         printf("\n street_address:");
  177.         printf("\n%s",db_student.a.street_address);
  178.         printf("\n temp_adreess:");
  179.         printf("\n%s",db_student.a.temp_address);
  180.         printf("\n perm_address:");
  181.         printf("\n%s",db_student.a.perm_address);
  182.         printf("\n date of birth");
  183.         printf("\n year");
  184.         printf("%d",db_student.d_o_b.yy);
  185.         printf("\n month");
  186.         printf("%d",db_student.d_o_b.mm);
  187.         printf("\n day");
  188.         printf("%d",db_student.d_o_b.dd);
  189.         printf("\n contact:");
  190.         printf("%d",db_student.contact);
  191.         printf("\n e_mail");
  192.         printf("%d",db_student.e_mail);
  193.         }
  194.         }
  195.     }
  196. }
  197. //checking for roll no.
  198. int student_available(int r)
  199. {
  200.     FILE *fp;
  201.     fp=fopen("student_info_file.dat","r");
  202.     while(!feof(fp))
  203.     {
  204.         fread(&db_student,sizeof(db_student),1,fp);
  205.             if(r==db_student.roll)
  206.             {
  207.                 fclose(fp);
  208.                 return 0;
  209.             }
  210.     }
  211. fclose(fp);
  212. return 1;
  213. }
  214. //function to view parents information
  215. void view_parents_info(char c[])
  216. {
  217. FILE *fp;
  218. fp=fopen("student_info_file.dat","r");
  219. int confirm;
  220. while(!feof(fp))
  221. {
  222.     fread(&db_student,sizeof(db_student),1,fp);
  223.     if(strcmp(c,db_parents.f_name)==0)
  224.     {
  225.     printf("\n first name:");
  226.     printf("\n%s",db_parents.f_name);
  227.     printf("\n last name:");
  228.     printf("\n%s",db_parents.l_name);
  229.     printf("\n country:");
  230.     printf("\n%s",db_parents.a.country);
  231.     printf("\n city:");
  232.     printf("\n%s",db_parents.a.city);
  233.     printf("\n street_address:");
  234.     printf("\n%s",db_parents.a.street_address);
  235.     printf("\n temp_adreess:");
  236.     printf("\n%s",db_parents.a.temp_address);
  237.     printf("\n perm_address:");
  238.     printf("\n%s",db_parents.a.perm_address);
  239.     printf("\n contact:");
  240.     printf("%s",db_parents.contact);
  241.     printf("\n e_mail");
  242.     printf("%s",db_parents.e_mail);
  243.     fclose(fp);
  244.     break;
  245.     }
  246. }
  247. //function to search the given record
  248. void search()
  249. {
  250.   FILE *fp;
  251.   int r,s,available;
  252.   printf("enter the roll number to search:");
  253.   scanf("%d",&r);
  254.   available=isRollAvailabe(r);
  255.   if(available==0)
  256.       printf("No record found for roll %d.",r);
  257.   else
  258.   {
  259.       fp=fopen("Record","rb");
  260.       while(fread(&db_student,sizeof(db_student),1,fp))
  261.       {
  262.           s=db_student.roll;
  263.           if(s==r)
  264.           {
  265.               printf("\n Roll: %d",db_student.roll);
  266.               printf("\n first name:%s",db_student.f_name);
  267.               printf("\n last name:%s",db_student.l_name);
  268.               printf("\n program:%s",db_student.program);
  269.               printf("\n level:%s",db_student.level);
  270.               printf("\n country:%s",db_student.a.country);
  271.               printf("\n city:%s",db_student.a.city);
  272.               printf("\n street_address:%s",db_student.a.street_address);
  273.               printf("\n temp_address:%s",db_student.a.temp_address);
  274.               printf("\n perm_address:%s",db_student.a.perm_address);
  275.               printf("\n date of birth");
  276.               printf("\n year");
  277.               printf("\n year:%d",db_student.d_o_b.yy);
  278.               printf("\n month:%d",db_student.d_o_b.mm);
  279.               printf("\n day:%d",db_student.d_o_b.dd);
  280.               printf("\n contact:%d",db_student.contact);
  281.               printf("\n e_mail:%d",db_student.e_mail);
  282.           }
  283.       }
  284.       fclose(fp);
  285.    }
  286.  }
  287. //function to delete record
  288. void deleteRecord()
  289. {
  290.  FILE *fp, *fp2;
  291.  int r,s, available;
  292.  printf("Enter the roll number to delete record: ");
  293.  scanf("%d", &r);
  294.  available =isRollavailable(r);
  295.  if(available==0)
  296.     printf("no record to delete for roll %d.",r);
  297.  else
  298.  {
  299.   fp=fopen("Record","rb");
  300.   fp2=fopen("Tempfile","wb");
  301.   while(fread(&db_student,sizeof(db_student),1,fp))
  302.   {
  303.       s=db_student.roll;
  304.       if(s !=r)
  305.         fwrite(&db_student,sizeof(db_student),1,fp2);
  306.   }
  307.   fclose(fp);
  308.   fclose(fp2);
  309.   fp=fopen("Record","wb");
  310.   fp2=fopen("Tempfile","rb");
  311.    while(fread(&db_student,sizeof(db_student),1,fp2))
  312.    {
  313.       fwrite(&db_student,sizeof(db_student),1,fp);
  314.    }
  315.    printf("\n Record Deleted");
  316.    fclose(fp);
  317.    fclose(fp2);
  318.  }
  319. }
  320. //function to update record
  321. void update()
  322. {
  323.     FILE *fp1, *fp2;
  324.     int r, s, available;
  325.     printf("enter the roll number to update record:");
  326.     scanf("%d", &r);
  327.     available=isRollAvailable(r);
  328.     if(available==0)
  329.         printf("no record to delete for roll %d.",r);
  330.  else
  331.  {
  332.   fp=fopen("Record","rb");
  333.   fp2=fopen("Tempfile","wb");
  334.   while(fread(&db_student,sizeof(db_student),1,fp))
  335.   {
  336.       s=db_student.roll;
  337.       if(s !=r)
  338.         fwrite(&db_student,sizeof(db_student),1,fp2);
  339.         else
  340.         {
  341.             printf("\n Updating data for previous roll %d",r);
  342.             printf("\n enter new roll:");
  343.             scanf("%d",&db_student.roll);
  344.             fflush(stdin);
  345.             printf("\n enter new name:");
  346.             scanf("%d",&db_student.f_name);
  347.             fflush(stdin);
  348.             printf("\n enter new first name:");
  349.             gets(db_student.f_name);
  350.             fflush(stdin);
  351.             printf("\n enter new last name:");
  352.             gets(db_student.l_name);
  353.             fflush(stdin);
  354.             printf("\n enter new program:");
  355.             gets(db_student.program);
  356.             fflush(stdin);
  357.             printf("\n enter new  level:");
  358.             gets(db_student.level);
  359.             fflush(stdin);
  360.             printf("\n enter new  country:");
  361.             gets(db_student.a.country);
  362.             fflush(stdin);
  363.             printf("\n enter new city:");
  364.             gets(db_student.a.city);
  365.             fflush(stdin);
  366.             printf("\n enter new  street_address:");
  367.             gets(db_student.a.street_address);
  368.             fflush(stdin);
  369.             printf("\n enter new temp_address:");
  370.             gets(db_student.a.temp_address);
  371.             fflush(stdin);
  372.             printf("\n enter  new perm_address:");
  373.             gets(db_student.a.perm_address);
  374.             fflush(stdin);
  375.             printf("\n enter new d.o.b:");
  376.             printf("\n enter new year:");
  377.             scanf("%d",&db_student.d_o_b.yy);
  378.             printf("\n enter new month:");
  379.             scanf("%d",&db_student.d_o_b.mm);
  380.             printf("\n enter new day:");
  381.             scanf("%d",db_student.d_o_b.dd);
  382.             printf("\n enter new  contact:");
  383.             scanf("%d",&db_student.contact);
  384.             fflush(stdin);
  385.             printf("\n enter new e_mail:");
  386.             gets(db_student.e_mail);
  387.             fflush(stdin);
  388.             printf("\n new gender?(M/F)");
  389.             gets(db_student.gender);
  390.             fwrite(&db_student,sizeof(db_student),1,fp);
  391.         }
  392.     fclose(fp);
  393.     fclose(fp2);
  394.     fp=fopen("Record","rb");
  395.     fp2=fopen("Tempfile","wb");
  396.     while(fread(&db_student,sizeof(db_student),1,fp2))
  397.     {
  398.         fwrite(&db_student,sizeof(db_student),1,fp);
  399.     }
  400.     printf("\n Record Updated");
  401.     fclose(fp);
  402.     fclose(fp2);
  403.     }
  404.   }
  405. }
  406.  
  407.  
  408. int main()
  409. {
  410.   int ch;
  411.   //FILE *fp;
  412.   //fp=fopen("record","w");
  413.   while(1)
  414.   {
  415.     printf("\n----------------------------------------------------------\n");
  416.     printf("\n User Menu:");
  417.     printf("\n \t1.Add Student Information\n \t2.Add Parents Information\n \t3.View Student Information\n \t4.View Parents Information\n\n");
  418.     printf("enter your choice:");
  419.     scanf("%d",&ch);
  420.     switch(ch)
  421.     {
  422.     case 1:
  423.         student_info_write();
  424.     case 2:
  425.         parents_info_write();
  426.     case 3:
  427.         print_student_info();
  428.     case 4:
  429.         print_parents_info();
  430.  
  431.  
  432.  
  433.     }
  434.   {
  435.  
  436.   }
  437.     return 0;
  438. }
  439. int main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement