Elementary Database Management System using C

Here is a menu driven program for elementary database management system using C. There is a provision to Add, Modify, List and Delete records, the operations that are imperative in any database management. Following comments would help you in understand the program easily:

  • Addition of record must always take place at the end of existing records in the file, much in the same way you would add new records in a register manually.
  • Listing records means displaying the existing record on the screen. Naturally, record should be listed from first record to last record.
  • While modifying records, first we must ask the user which record he intends to modify. Instead of asking the record number to be modified, it would be more meaningful to ask for the name of the employee whose record is to be modified. On modifying the record, the existing record gets overwritten by the new record.
  • In deleting records, except for the record to be deleted, rest of the records must first be written to a temporary file must be renamed back to original.
  • Observe carefully the way the file has been opened, first for reading and writing, and if this fails (the first time you run this program it would certainly fail, because that time the file is not existing), for writing and reading. It is imperative that the file should be opened in binary mode.
  • Note that the file is being opened only once and closed only once, which is quite logical.

Software Code for Elementary Database Management System using C

 

To understand how this program works, you need to be familiar with the concept of pointer. A pointer is initiated whenever we open a file. On opening a file a pointer is set up which points to the first record in the file. On using the functions fread() or fwrite(), the pointer moves to the beginning of the next record. On closing a file the pointer is deactivated.

The rewind() function places the pointer to the beginning of the file, irrespective of where it is present right now.

Note that the pointer movement is of utmost importance since fread() always reads that records where the pointer is currently placed. Similarly, fwrite() always the record where the pointer is currently placed.

The fseek() function lets us move the pointer from one record to another. In the program above, to move the pointer to the previous record from its current position, we used the function,

fseek(fp, -recsize, SEEK_CUR);

Here, -recsize moves the pointer back by recsize bytes from the current position. SEEK_CUR is a macro defined in “stdio.h”.

Similarly, the following fseek() would place the pointer beyond the last record in the file.

fseek(fp, 0, SEEK_END);

In fact -recsize or 0 are just the offsets that tell the compiler by how many bytes should the pointer be moved from a particular position. The third argument could be SEEK_END, SEEK_CUR or SEEK_SET. All these act as a reference from which the pointer should be offset. SEEK_END means move the pointer from the end of the file, SEEK_CUR means move the pointer with reference to its current position and SEEK_SET means move the pointer with reference to the beginning of the file.

If we wish to know where the pointer is positioned right now, we can use the function ftell(). It returns this position as a long int which is an offset from the beginning of the file. The value returned by ftell() can be used in subsequent calls to fseek(). A sample call to ftell() is shown below.

position=ftell(fp);

where position is a long int.

One Thought to “Elementary Database Management System using C”

  1. Logeshwaran

    Respected website Admin sir,

    Please post some projects using python programming language.

Leave a Comment