Find Student Function
a function that searchs a student in database and returns his file name
1. Function Definition:
char* findStudent(char* nom, char* prenom)Code snippet:
char* findStudent(char* nom, char* prenom) {
char option1[100];
char option2[100];
FILE* tmp1;
FILE* tmp2;
sprintf(option1, "database/%s_%s.txt", nom, prenom);
sprintf(option2, "database/%s_%s.txt", prenom, nom);
tmp1 = fopen(option1, "r");
tmp2 = fopen(option2, "r");
if (tmp1 != NULL) {
fclose(tmp1);
if (tmp2 != NULL) {
fclose(tmp2);
}
char* result = malloc(strlen(option1) + 1); // Allocate memory for the result
strcpy(result, option1); // Copy option1 to result
return result;
} else if (tmp2 != NULL) {
fclose(tmp2);
char* result = malloc(strlen(option2) + 1); // Allocate memory for the result
strcpy(result, option2); // Copy option2 to result
return result;
}
return NULL;
}2. Variable Declarations:
Variable
Description
3. Function Tasks:
3.1 Generating File Paths:
3.2 Opening Files:
3.3 File Existence Checking:
Last updated