Guest User

Untitled

a guest
May 24th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. int main()
  2. {
  3.     student *studentsArray;
  4.     char *inputFile = malloc(100);
  5.     char *outputFile = malloc(100);
  6.     char line[100];
  7.     FILE *dataFile;
  8.    
  9.     int studentCount = 0;
  10.     int lineCount = 0;
  11.     int i, j = 0;
  12.    
  13.     fputs("Please enter file to open ", stdout);
  14.     fflush(stdout);
  15.    
  16.     if(fgets(inputFile, sizeof inputFile, stdin) != NULL)
  17.     {
  18.         printf(inputFile);
  19.         char *newline = strchr(inputFile, '\n');
  20.         if( newline != NULL)
  21.         {
  22.             *newline = '\0';
  23.         }
  24.     }
  25.    
  26.     dataFile = fopen(inputFile, "r");
  27.    
  28.     if(dataFile == NULL)
  29.     {
  30.         fprintf(stderr, "Unable to read text file");
  31.         return 1;
  32.     }
  33.    
  34.     while(fgets(line, 100, dataFile) != NULL)
  35.     {
  36.         switch(lineCount)
  37.         {
  38.             case 0:
  39.                 strcpy(studentsArray[studentCount].studentId, line);
  40.                 lineCount++;
  41.                 break;
  42.             case 1:
  43.                 strcpy(studentsArray[studentCount].studentName, line);
  44.                 lineCount++;
  45.                 break;
  46.             case 2:
  47.                 studentsArray[studentCount].studentGrades[j] = atof(strtok(line, " "));
  48.                 while(studentsArray[studentCount].studentGrades[j] != NULL)
  49.                 {
  50.                     studentsArray[studentCount].studentGrades[j] = atof(strtok(NULL, " "));
  51.                     j++;
  52.                 }
  53.                 lineCount = 0;
  54.                 j = 0;
  55.                 studentCount++;
  56.                 break;
  57.         }
  58.     }
  59.    
  60.     fclose(dataFile);
  61.    
  62.     // Get all the calculated final grades for each student
  63.     studentsArray = calculateFinalGrades(studentsArray);
  64.    
  65.     fputs("Please enter file to output to ", stdout);
  66.     fflush(stdout);
  67.    
  68.     if(fgets(outputFile, sizeof(outputFile), stdin) != NULL)
  69.     {
  70.         char *newline = strchr(outputFile, '\n');
  71.         if( newline != NULL)
  72.         {
  73.             *newline = '\0';
  74.         }
  75.     }
  76.    
  77.     printStudentGrades(studentsArray, outputFile);
  78.    
  79.     return 0;
  80. }
Add Comment
Please, Sign In to add comment