This project provides a command-line tool to manage a simple database of employees stored in a binary file. It enables you to create, read, update, and delete employee records in the database. The tool operates by loading the database file into memory, performing operations on it, and writing the results back to disk.
├── bin
│ └── dbview # Compiled binary for the database viewer
├── include
│ ├── common.h # Common constants and utility macros
│ ├── file.h # File handling functions declarations
│ └── parse.h # Parsing and database manipulation declarations
├── Makefile # Build system file for compiling the project
├── mynewdb.db # Example database file
├── obj
│ ├── file.o # Compiled object file for file.c
│ ├── main.o # Compiled object file for main.c
│ └── parse.o # Compiled object file for parse.c
├── README.md # Project documentation (this file)
└── src
├── file.c # Implementation of file handling functions
├── main.c # Main program entry point
└── parse.c # Implementation of parsing and manipulation functions
-
Opening or Creating the Database File:
- The
main.c
file handles command-line arguments to determine if a new database file should be created or an existing one opened. - The
create_db_file
andopen_db_file
functions infile.c
manage the interaction with the filesystem.
- The
-
Validating and Loading the Database Header:
- If a new file is created,
create_db_header
initializes the database header. - For existing files,
validate_db_header
ensures the file integrity by checking its header.
- If a new file is created,
-
Reading Employee Records into Memory:
- The
read_employees
function inparse.c
loads employee records from the database file into memory. It also handles necessary endian conversions for portability.
- The
-
Performing Operations on the Database in Memory:
- Depending on the command-line arguments:
- Adding an employee (
add_employee
) - Listing all employees (
list_employees
) - Updating an employee’s record (
update_employee
) - Removing an employee’s record (
remove_employee
)
- Adding an employee (
- These operations manipulate the in-memory representation of the database.
- Depending on the command-line arguments:
-
Writing Changes Back to Disk:
- After all operations are complete, the
output_file
function writes the updated database header and employee records back to the file. - The file size is adjusted using
ftruncate
if records are removed.
- After all operations are complete, the
-
Add Employee: Add a new employee with their name, address, and working hours using the
-a
option../bin/dbview -f mynewdb.db -a "John Doe, 123 Elm St, 40"
-
List Employees: Display all employees in the database with the
-l
option../bin/dbview -f mynewdb.db -l
-
Update Employee: Update the working hours of a specific employee using the
-u
option../bin/dbview -f mynewdb.db -u
Follow the prompts to specify the employee and new hours.
-
Remove Employee: Remove an employee from the database using the
-r
option with the employee's name../bin/dbview -f mynewdb.db -r "John Doe"
-
Create New Database: Create a new database file with the
-n
option../bin/dbview -n -f mynewdb.db
- Ensure you have a C compiler installed (e.g.,
gcc
). - Run the
Makefile
to compile the project:make
- The compiled binary will be available in the
bin/
directory asdbview
.
common.h
: Contains common constants and return status macros.file.h
: Declares functions for file handling (e.g., creating, opening database files).parse.h
: Declares functions for manipulating database records (e.g., add, remove, update).
file.c
: Implements file handling functions to manage interaction with the filesystem.main.c
: The entry point of the program; handles argument parsing and orchestrates database operations.parse.c
: Implements database manipulation functions, including reading, writing, and updating records.
-
Create a New Database:
./bin/dbview -n -f mynewdb.db
-
Add an Employee:
./bin/dbview -f mynewdb.db -a "Alice, 456 Maple St, 35"
-
List Employees:
./bin/dbview -f mynewdb.db -l
-
Update an Employee:
./bin/dbview -f mynewdb.db -u
-
Remove an Employee:
./bin/dbview -f mynewdb.db -r "Alice"
- Ensure the database file (
mynewdb.db
) is not deleted or corrupted, as this could result in data loss or program errors. - The database uses a simple binary format; manual edits to the file may corrupt it.