Database In C Is Killing Me!
- Boltrig
-
Boltrig
- Member since: Mar. 17, 2006
- Offline.
-
- Forum Stats
- Member
- Level 17
- Blank Slate
Ive been given this assignment to do, and I have no idea how to go about it. Unluckily the coin toss gave me module B to do which is the hardest!
Module B
Overview:
This module performs the storage and retrieval of all records. The records reside in memory only. On demand, data are passed back to the user-interface module for display.
This module implements only the records database, not user interface.
Module B Application Programming Interface (API):
Module B should provide the public routines specified below. These routines must not use the user interface in any way except in case of debugging. Nothing else is acceptable.
In addition:
Module B must use one file of code only named: database.c
All function prototypes for module B must be declared in a header file database.h, so that userInterface.c can include this.
The following definition must be also provided in database.h:
typedef struct {
char surname[21], /* student surname */
char firstname[21], /* buffer to receive message */
char studentID[7], /* student identifier */
int studentAge /* age of student in years */
} STUDENT_RECORD;
Public routines:
studentRecordNew( )
NAME
studentRecordNew ( ) create a new student record
SYNOPSIS
Int studentRecordNew
(STUDENT_RECORD * studentRecord ; /* IN: the data for the new record */)
DESCRIPTION
This routine creates a new student record in the database. It checks if the student already exists.
RETURNS
True: if a new record is created.
False: if student ID already exists. No record is created.
studentRecordUpdate( )
NAME
studentRecordUpdate ( ) update a new student record
SYNOPSIS
Int studentRecordUpdate
(STUDENT_RECORD * studentRecord ; /* IN: the update data */)
DESCRIPTION
This routine updates a student record in the database. It checks if the student already exists. Any parameters that are set will be used to update the record. Unused string parameters must be set to the empty string '\0'. Unused studentAge parameter must be set to -1.
RETURNS
True: if the record is updated.
False: if student ID does not exist or any other problem occurs.
studentRecordGet( )
NAME
studentRecordGet ( ) returns a student record
SYNOPSIS
Int studentRecordGet
(char * studentID; /* IN: id of record to be retrieved */
STUDENT_RECORD * studentRecord ; /* OUT: the student record data */)
DESCRIPTION
Takes as input parameter a possible student ID. The routine checks whether the student exists. If the student exists then the routine returns the data in parameter studentRecord. This function can be used to get the data or just to check that a student exists.
RETURNS
True: if the student exists in the database.
False: if student ID does not exist.
studentRecordDelete( )
NAME
studentRecordDelete( ) deletes a student record from the database
SYNOPSIS
Int studentRecordDelete
(char * studentID; /* IN: id of record to be deleted */)
DESCRIPTION
Takes as input parameter a possible student ID. The routine checks if the student exists. If the student exists then the routine deletes the student from the database.
RETURNS
True: if the deletion is successful.
False: if student ID does not exist.
I would really apreciate any help you can give me. My problems are mainly:
I have no idea what should go in the header file and
How to store the records (array? etc)
- CronoMan
-
CronoMan
- Member since: Jul. 19, 2004
- Offline.
-
- Forum Stats
- Member
- Level 06
- Blank Slate
At 11/29/07 04:41 AM, Boltrig wrote: I have no idea what should go in the header file and
How to store the records (array? etc)
Well, since you're using C, an array would suffice
if you have a struct which contain data, you'll just do it like this :
int element_count = 0;
somestruct *elements = NULL;
void AddRecord(somestruct *data)
{
element_count++;
elements = (somestruct *)realloc(sizeof(somestruct) * element_count);
memcpy(&elements[element_count - 1], data, sizeof(somestruct));
}
which would allow dynamic allocation of space without having to rebuild the list :)
"no sound in ass"
- Boltrig
-
Boltrig
- Member since: Mar. 17, 2006
- Offline.
-
- Forum Stats
- Member
- Level 17
- Blank Slate
Does the void and int at the start of the function make a difference? The specification lists them as being ints..
- CronoMan
-
CronoMan
- Member since: Jul. 19, 2004
- Offline.
-
- Forum Stats
- Member
- Level 06
- Blank Slate
At 11/29/07 05:27 AM, Boltrig wrote: Does the void and int at the start of the function make a difference? The specification lists them as being ints..
int means that the function returns a integer, whereas void doesn't return any value at all.
If the specification calls for int, I guess you could return the number of records inserted, or the current index or something...
"no sound in ass"
- White-Rhyno
-
White-Rhyno
- Member since: Apr. 28, 2003
- Offline.
-
- Forum Stats
- Member
- Level 38
- Blank Slate
At 11/29/07 05:21 AM, CronoMan wrote: int element_count = 0;
somestruct *elements = NULL;
GLOBAL VARIABLES?!? You fool, you've doomed us all! Dijkstra save me!
- CronoMan
-
CronoMan
- Member since: Jul. 19, 2004
- Offline.
-
- Forum Stats
- Member
- Level 06
- Blank Slate
At 11/29/07 05:54 AM, White-Rhyno wrote:At 11/29/07 05:21 AM, CronoMan wrote: int element_count = 0;GLOBAL VARIABLES?!? You fool, you've doomed us all! Dijkstra save me!
somestruct *elements = NULL;
C, not C++ ;)
"no sound in ass"
- StrixVariaXIX
-
StrixVariaXIX
- Member since: Nov. 18, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Gamer
At 11/29/07 05:39 AM, CronoMan wrote: If the specification calls for int, I guess you could return the number of records inserted, or the current index or something...
C standard is that any function return 0 for success or some negative value for an error. The negative value can vary depending on the kind of error.
#define SOME_ERROR -1
#define SOME_OTHER_ERROR -2
Use those, and put those and the function prototypes in the header so the user can see it. Then when you compile into a library, the user only sees what he needs to to understand how to use the module.
Use some kind of standard for your error names.
- Boltrig
-
Boltrig
- Member since: Mar. 17, 2006
- Offline.
-
- Forum Stats
- Member
- Level 17
- Blank Slate
Thanks to all you guys that gave me tips throughout. In the end I scrapped the entire program and recoded a linked list in about 4 hours. It didnt work right, but it got me a pass.

