Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs503zchen
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ziheng Chen
cs503zchen
Commits
8ccbc857
Commit
8ccbc857
authored
5 months ago
by
Ziheng Chen
Browse files
Options
Downloads
Patches
Plain Diff
Edit sdbsc.c
parent
cd9d9341
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
w4hw/codes/sdbsc.c
+7
-7
7 additions, 7 deletions
w4hw/codes/sdbsc.c
with
7 additions
and
7 deletions
w4hw/codes/sdbsc.c
+
7
−
7
View file @
8ccbc857
...
...
@@ -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"
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment