Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void checkFile(const char *key, const char *st)
- {
- // Opening files:
- // create file path strings
- char *keyPath = malloc((PATH_SZ+strlen(key)) * sizeof(char));
- if (!keyPath) die("Failed to follow key path");
- memcpy(keyPath, PATH, PATH_SZ);
- memcpy(keyPath+PATH_SZ, key, strlen(key));
- char *stPath = malloc((PATH_SZ+strlen(st)) * sizeof(char));
- if (!stPath) die("Failed to follow key path");
- memcpy(stPath, PATH, PATH_SZ);
- memcpy(stPath+PATH_SZ, st, strlen(st));
- // open files
- FILE *keyFile = fopen(keyPath, "r");
- if (!keyFile) die("Failed to open key file");
- FILE *stFile = fopen(stPath, "r");
- if (!stFile) die("Failed to open student file");
- // Comparing files:
- char keyLine[LINE_SZ]; // strings of each line of the file
- char stLine[LINE_SZ];
- // compare first lines
- int cont = 1;
- float grade;
- fgets(keyLine, LINE_SZ, keyFile);
- fgets(stLine, LINE_SZ, stFile);
- if (strcmp(stLine, keyLine) != 0) {
- cont = 0;
- grade = 0;
- // fprintf(stdout, "%s.2f\n", "Grade:", grade);
- printf("Grade:.2f\n", grade);
- }
- if (cont == 1) {
- int correct = 1;
- int total = 1;
- // compare the rest of the lines
- while (fgets(keyLine, LINE_SZ, keyFile)) {
- fgets(stLine, LINE_SZ, stFile);
- if (strcmp(keyLine, stLine) == 0) {
- correct++; // if lines are the same inc num correct
- }
- total++;
- }
- grade = round((float) correct / total * 100);
- // fprintf(stdout, "%s.2f\n", "Grade:", grade);
- printf("Grade:.2f\n", grade);
- }
- //Cleanup:
- free(keyPath);
- free(stPath);
- fclose(keyFile);
- fclose(stFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement