Skip to content
Snippets Groups Projects
Commit db5462f8 authored by Joey Le's avatar Joey Le
Browse files

"Finishd all of the functions "

parent ad889bbc
Branches
No related tags found
No related merge requests found
...@@ -204,7 +204,7 @@ int del_student(int fd, int id) ...@@ -204,7 +204,7 @@ int del_student(int fd, int id)
myoffset = id * STUDENT_RECORD_SIZE; myoffset = id * STUDENT_RECORD_SIZE;
off_t lseek_result = lseek(fd, myoffset, SEEK_SET); off_t lseek_result = lseek(fd, myoffset, SEEK_SET);
if (lseek_result) { if (lseek_result < 1) {
printf(M_ERR_DB_READ); printf(M_ERR_DB_READ);
return ERR_DB_FILE; return ERR_DB_FILE;
} }
...@@ -246,9 +246,41 @@ int del_student(int fd, int id) ...@@ -246,9 +246,41 @@ int del_student(int fd, int id)
*/ */
int count_db_records(int fd) int count_db_records(int fd)
{ {
// TODO int count = 0;
printf(M_NOT_IMPL); student_t temp;
return NOT_IMPLEMENTED_YET; ssize_t bytes;
off_t lseek_result = lseek(fd, 0, SEEK_SET );
if (lseek_result < 0 ) {
printf(M_ERR_DB_READ);
return ERR_DB_FILE;
}
while (1)
{
bytes = read(fd, &temp, sizeof(student_t) );
if (bytes == -1) {
return ERR_DB_FILE;
} else if (bytes == 0) {
break;
} else if (bytes != STUDENT_RECORD_SIZE) {
return ERR_DB_FILE;
}
if (memcmp(&temp, &EMPTY_STUDENT_RECORD, STUDENT_RECORD_SIZE) != 0) {
count++;
}
}
if (count == 0){
printf(M_DB_EMPTY);
} else {
printf(M_DB_RECORD_CNT, count);
}
return count;
} }
/* /*
...@@ -286,11 +318,53 @@ int count_db_records(int fd) ...@@ -286,11 +318,53 @@ int count_db_records(int fd)
*/ */
int print_db(int fd) int print_db(int fd)
{ {
// TODO student_t temp;
printf(M_NOT_IMPL); ssize_t bytes;
return NOT_IMPLEMENTED_YET; int first_record = 1;
off_t lseek_result = lseek(fd, 0, SEEK_SET );
if (lseek_result < 0 ) {
printf(M_ERR_DB_READ);
return ERR_DB_FILE;
} }
while (1)
{
bytes = read(fd, &temp, sizeof(student_t) );
if (bytes == -1) {
printf(M_ERR_DB_READ);
return ERR_DB_FILE;
} else if (bytes == 0) {
break;
} else if (bytes != STUDENT_RECORD_SIZE) {
printf(M_ERR_DB_READ);
return ERR_DB_FILE;
}
if (memcmp(&temp, &EMPTY_STUDENT_RECORD, STUDENT_RECORD_SIZE) == 0) {
continue;
}
if (first_record) {
printf(STUDENT_PRINT_HDR_STRING, "ID", "FIRST NAME", "LAST_NAME", "GPA");
first_record = 0;
}
float gpa = temp.gpa / 100.0;
printf(STUDENT_PRINT_FMT_STRING, temp.id, temp.fname, temp.lname, gpa);
}
if (first_record) {
printf(M_DB_EMPTY) ;
}
return NO_ERROR;
}
/* /*
* print_student * print_student
* *s: a pointer to a student_t structure that should * *s: a pointer to a student_t structure that should
...@@ -321,8 +395,17 @@ int print_db(int fd) ...@@ -321,8 +395,17 @@ int print_db(int fd)
*/ */
void print_student(student_t *s) void print_student(student_t *s)
{ {
// TODO if (s == NULL || s-> id == 0 ) {
printf(M_NOT_IMPL); printf(M_ERR_STD_PRINT);
return;
}
printf(STUDENT_PRINT_HDR_STRING, "ID", "FIRST NAME", "LAST_NAME", "GPA");
float gpa = s->gpa / 100.0;
printf(STUDENT_PRINT_FMT_STRING, s->id, s->fname, student.lname, calculated_gpa_from_s);
} }
/* /*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment