Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C Program to Create a Student Database Using Structure



Hello Friend, Many of you were contacting me via social medias asking for C Program to Create a Student Database Using Structure. So here is the code to make a student database.

Note:
./a.out  studentDB.txt => passing file name(studentDB.txt) in command line.

Code:


  1.   #include
  2.   #include
  3.   #include
  4.   /* student structure */
  5.   struct student {
  6.         char name[32];
  7.         int age, rollNo;
  8.         int m1, m2, m3;
  9.         float average;
  10.   };
  11.   int main(int argc, char *argv[]) {
  12.         struct student s1;
  13.         int i, n, m1, m2, m3, age, flag, rollNo;
  14.         char name[32];
  15.         float average;
  16.         FILE *fp;
  17.         /* open the given input file in write mode  */
  18.         fp = fopen(argv[1], "w");
  19.         /* get the number of students from the user */
  20.         printf("Enter the number of students:");
  21.         scanf("%d", &n);
  22.         /* get student information from user and write it to a file */
  23.         for (i = 0; i
  24.                 printf("\nEnter student %d details:\n", i + 1);
  25.                 getchar();
  26.                 printf("Name: ");
  27.                 fgets(name, 32, stdin);
  28.                 name[strlen(name) - 1] = '\0';
  29.                 printf("Roll Number: ");
  30.                 scanf("%d", &rollNo);
  31.                 printf("Age: ");
  32.                 scanf("%d", &age);
  33.                 printf("Mark1, Mark2, Mark3:");
  34.                 scanf("%d%d%d", &m1, &m2, &m3);
  35.                 average = (1.0 * (m1 + m2 + m3)) / 3;
  36.                 /* writing student information to a file  */
  37.                 fprintf(fp, "%d %s %d %d %d %d %f\n",
  38.                         rollNo, name, age, m1, m2, m3, average);
  39.         }
  40.         /* closing the opened file */
  41.         fclose(fp);
  42.         /* opening the input file in read mode */
  43.         fp = fopen(argv[1], "r");
  44.         /*
  45.          * fetches the desired student record from
  46.          * file and displays it to the user 
  47.          */
  48.         while (1) {
  49.                 printf("\nEnter the desired student roll number:");
  50.                 scanf("%d", &rollNo);
  51.                 flag = 0;
  52.                 while (!feof(fp)) {
  53.                         fscanf(fp, "%d %s %d %d %d %d %f", &s1.rollNo,
  54.                                 s1.name, &s1.age, &s1.m1, &s1.m2, 
  55.                                               &s1.m3, &s1.average);
  56.                         if (s1.rollNo == rollNo) {
  57.                                 printf("Details of %s:\n", s1.name);
  58.                                 printf("RollNo: %d\n", s1.rollNo);
  59.                                 printf("Age: %d\n", s1.age);
  60.                                 printf("Mark1: %d\n", s1.m1);
  61.                                 printf("Mark2: %d\n", s1.m2);
  62.                                 printf("Mark3: %d\n", s1.m3);
  63.                                 printf("Average: %f\n", s1.average);
  64.                                 flag = 1;
  65.                                 break;
  66.                         }
  67.                 }
  68.                 if (!flag) {
  69.                         printf("Record Not Found!!\n");
  70.                 }
  71.                 /* rewinding the file pointer to the start of file */
  72.                 rewind(fp);
  73.                 printf("Do you want to continue(1/0):");
  74.                 scanf("%d", &flag);
  75.                 if (!flag)
  76.                         break;
  77.         }
  78.         /* closing the file */
  79.         fclose(fp);
  80.         return(0);
  81.   }

Output:


 [email protected]:~/$ ./a.out   studentDB.txt 
  Enter the number of students:3

  Enter student 1 details:
  Name: Ram
  Roll Number: 101
  Age: 10
  Mark1, Mark2, Mark3:100 100 100

  Enter student 2 details:
  Name: Sam
  Roll Number: 102
  Age: 10
  Mark1, Mark2, Mark3:95 96 97

  Enter student 3 details:
  Name: Tom
  Roll Number: 103
  Age: 10
  Mark1, Mark2, Mark3:95 97 99

  Enter the desired student roll number:101
  Details of Ram:
  RollNo: 101
  Age: 10
  Mark1: 100
  Mark2: 100
  Mark3: 100
  Average: 100.000000
  Do you want to continue(1/0):1

  Enter the desired student roll number:103
  Details of Tom:
  RollNo: 103
  Age: 10
  Mark1: 95
  Mark2: 97
  Mark3: 99
  Average: 97.000000
  Do you want to continue(1/0):0

  [email protected]:~/$ cat studentDB.txt 
  101 Ram 10 100 100 100 100.000000
  102 Sam 10   95  96   97  96.000000
  103 Tom 10  95  97   99   97.000000 



This post first appeared on TechJunior, please read the originial post: here

Share the post

C Program to Create a Student Database Using Structure

×

Subscribe to Techjunior

Get updates delivered right to your inbox!

Thank you for your subscription

×