Sunday, July 18, 2010

File Database I/O

I have enrolled to the Database Management Systems (DBMS) course in the present semester. The first lab assignment was to model a file DB - use file to store, retrieve, modify and delete structured data (name, roll number). This assignment was to understand the difficulties involved in managing structured data using the conventional file system.

The program
The C program gives options to insert, retrieve and delete data from a file DB - db.dat. The source code is available here.

There is comment on the important sections of the code and I hope you could understand most of it.


/*
* Coded By: Rahul Krishnan
*
* Description:
* Program implementing basic Database I/O
* using file system instead of DBMS. This
* is to show that files are not designed
* to store structured and related data.
*/

#include
#include
#include

int
main() {
char name[10];
int choice, roll, key, found = 0;
FILE *f_ptr; // File pointer to db.dat
FILE *n_ptr; // File pointer to newdb.dat

while (1) {
printf("\n***** FILE DB Menu ******");
printf("\n1. Enter data\n2. Display All\n3. Search by roll\n4. Delete by roll\n5. Exit");
printf("\nEnter your choice : ");
scanf("%d", &choice);

switch (choice) {
case 1:
// Adds an entry into the file DB.
f_ptr = fopen("db.dat", "a");

printf("Enter the : ");

// Read from stdin and write to file.
fscanf(stdin, "%d %s", &roll, &name);
fprintf(f_ptr, "%d %s\n", roll, name);

fclose(f_ptr);
break;
case 2:
// Displays all the contents of the file DB.
printf("\nShowing DB\n----------\n");
f_ptr = fopen("db.dat", "r");

// Read from "db.dat" and write to stdout.
while ((fscanf(f_ptr, "%d %s", &roll, &name)) != EOF) {
printf("%d %s\n", roll, name);
}

fclose(f_ptr);
break;
case 3:
// Search the file DB with roll as the key.
printf("Enter the roll to be searched : ");
scanf("%d", &key);

f_ptr = fopen("db.dat", "r");
found = 0; // Set to 1 if record found.

// Search through all the entries comparing the key.
while ((fscanf(f_ptr, "%d %s", &roll, name)) != EOF) {
if (roll == key) {
printf("\nRecord Found\n-------------\n%d %s\n-------------", roll, name);
found = 1;
}
}

if (found != 1) {
printf("\nRecord NOT found !!");
}
fclose(f_ptr);

break;
case 4:
// Deletes a file DB entry based on the roll as key.
printf("Enter the ROLL of the entry to be deleted : ");
scanf("%d", &key);

f_ptr = fopen("db.dat", "r");
n_ptr = fopen("newdb.dat", "w");

found = 0; // Set to 1 if record found.

// Copy all the entries except the one with the key
// to a new file "newdb.dat", which is rename later to
// "db.dat".
while ((fscanf(f_ptr, "%d %s", &roll, name)) != EOF) {
if (roll != key) {
fprintf(n_ptr,"%d %s\n", roll, name);
}
if (roll == key)
found = 1;
}

// To rename the newdb.dat to db.dat.
fclose(f_ptr);
fclose(n_ptr);
remove("db.dat");
rename("newdb.dat", "db.dat");

// If the key is not found, then output NOT found.
// But the file is already duplicated and the file names
// changed, even if the key is not found.
if (found != 1) {
printf("\nRecord NOT found !!");
}

break;
case 5:
exit(0);
default:
printf("\nInvalid Choice");
}
}
return 0;
}

No comments:

Post a Comment