Skip to content
Snippets Groups Projects
Commit 8ccbc857 authored by Ziheng Chen's avatar Ziheng Chen
Browse files

Edit sdbsc.c

parent cd9d9341
No related branches found
No related tags found
No related merge requests found
......@@ -127,44 +127,44 @@ int add_student(int fd, int id, char *fname, char *lname, int gpa)
student_t student = {0}; // Empty struct to check existing record
off_t offset = id * STUDENT_RECORD_SIZE;
// 🔹 Validate ID and GPA Range
// Validate ID and GPA Range
if (validate_range(id, gpa) != NO_ERROR) {
printf(M_ERR_STD_RNG);
return ERR_DB_OP;
}
// 🔹 Move to correct position
// Move to correct position
if (lseek(fd, offset, SEEK_SET) < 0) {
perror("Error seeking in database file");
return ERR_DB_FILE;
}
// 🔹 Read existing student record
// Read existing student record
ssize_t bytes_read = read(fd, &student, STUDENT_RECORD_SIZE);
if (bytes_read < 0) {
perror("Error reading database file");
return ERR_DB_FILE;
}
// 🔹 If student already exists, return error
// If student already exists, return error
if (memcmp(&student, &EMPTY_STUDENT_RECORD, STUDENT_RECORD_SIZE) != 0) {
printf(M_ERR_DB_ADD_DUP, id);
return ERR_DB_OP;
}
// 🔹 Fill new student details
// Fill new student details
student.id = id;
student.gpa = gpa;
strncpy(student.fname, fname, sizeof(student.fname) - 1);
strncpy(student.lname, lname, sizeof(student.lname) - 1);
// 🔹 Move back before writing
// Move back before writing
if (lseek(fd, offset, SEEK_SET) < 0) {
perror("Error seeking in database file before writing");
return ERR_DB_FILE;
}
// 🔹 Write student record
// Write student record
ssize_t bytes_written = write(fd, &student, STUDENT_RECORD_SIZE);
if (bytes_written != STUDENT_RECORD_SIZE) {
perror("Error writing student record to database");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment