Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     void checkFile(const char *key, const char *st)
  2.     {
  3.         // Opening files:
  4.         // create file path strings
  5.         char *keyPath = malloc((PATH_SZ+strlen(key)) * sizeof(char));
  6.         if (!keyPath) die("Failed to follow key path");
  7.         memcpy(keyPath, PATH, PATH_SZ);
  8.         memcpy(keyPath+PATH_SZ, key, strlen(key));
  9.    
  10.         char *stPath =  malloc((PATH_SZ+strlen(st)) * sizeof(char));
  11.         if (!stPath) die("Failed to follow key path");
  12.         memcpy(stPath, PATH, PATH_SZ);
  13.         memcpy(stPath+PATH_SZ, st, strlen(st));
  14.  
  15.         // open files
  16.         FILE *keyFile = fopen(keyPath, "r");
  17.         if (!keyFile) die("Failed to open key file");
  18.  
  19.         FILE *stFile = fopen(stPath, "r");
  20.         if (!stFile) die("Failed to open student file");
  21.  
  22.         // Comparing files:
  23.         char keyLine[LINE_SZ];  // strings of each line of the file
  24.         char stLine[LINE_SZ];
  25.  
  26.         // compare first lines
  27.         int cont = 1;
  28.         float grade;
  29.         fgets(keyLine, LINE_SZ, keyFile);
  30.         fgets(stLine, LINE_SZ, stFile);
  31.         if (strcmp(stLine, keyLine) != 0) {
  32.             cont = 0;
  33.             grade = 0;
  34.             // fprintf(stdout, "%s.2f\n",  "Grade:", grade);
  35.             printf("Grade:.2f\n", grade);
  36.         }
  37.    
  38.         if (cont == 1) {
  39.             int correct = 1;
  40.             int total = 1;
  41.             // compare the rest of the lines
  42.             while (fgets(keyLine, LINE_SZ, keyFile)) {
  43.                 fgets(stLine, LINE_SZ, stFile);
  44.                 if (strcmp(keyLine, stLine) == 0) {
  45.                     correct++;  // if lines are the same inc num correct
  46.                 }
  47.                 total++;
  48.             }
  49.             grade = round((float) correct / total * 100);
  50.             // fprintf(stdout, "%s.2f\n",  "Grade:", grade);
  51.             printf("Grade:.2f\n", grade);
  52.         }
  53.    
  54.         //Cleanup:
  55.         free(keyPath);
  56.         free(stPath);
  57.         fclose(keyFile);
  58.         fclose(stFile);
  59.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement