This code implements a text-based database management system in C...
September 3, 2025 at 11:11 PM
This code implements a text-based database management system in C that allows a user to perform basic operations on a plaintext "database" stored in a file. Here's a breakdown of what the code does:
Key Functionalities:
-
Validation of User Input (
inputValido
):- Ensures that the user-provided key is only composed of alphanumeric characters (
a-z
,A-Z
,0-9
). - Uses regular expressions to validate the input.
- Ensures that the user-provided key is only composed of alphanumeric characters (
-
Check if a Key is Already Present (
giaPresente
):- Opens the database file in read mode.
- Iterates through its lines to check if the provided key (the left-hand side of the
key:value
pair) is already present. - Prints a message if the key is found.
-
Add a Record to the Database (
aggiungiRecord
):- Prompts the user for a key and a value.
- Validates the key and checks for duplicates.
- If the key is valid and not present, appends the key-value pair (in
key:value
format) to the database file.
-
Search for a Record by Key (
cercaRecord
):- Accepts a key as input.
- Searches the database for any records that match the key on the left-hand side of
key:value
pairs. - If found, it prints the entire matching line(s).
-
Sort the Database (
ordinaDatabase
):- Reads all lines from the database into an array of strings.
- Sorts the array in ascending lexicographical order using
qsort
. - Writes the sorted content back into the database file.
-
Display the Entire Database (
mostraDatabase
):- Prints all lines from the database file to the terminal.
-
Menu and User Interaction (
main
):- Implements a looped menu allowing the user to select:
- Add a new record.
- Search for a record by key.
- Sort the database.
- View all records.
- Exit the program.
- If the database file does not exist, it is created.
- Implements a looped menu allowing the user to select:
File Handling:
The database is stored as a text file (TEST/database.txt
). Each record is stored in the format:
key:value
Workflow:
- The user is presented with a menu to interact with the database.
- Based on the user's choice, an appropriate function is executed.
- Records are validated (e.g., for valid characters, duplicates) where necessary to ensure data integrity.
Error Handling:
- Handles errors such as invalid file operations (e.g., failure to open or write to the database file).
- Ensures proper memory management, like freeing dynamically allocated memory after sorting.
- Contains simple input validation to handle improper choices in the menu or invalid input for keys/values.
Example Use Case:
- Add a record:
key1:val1
. - Add another record:
key2:val2
. - Search for
key1
— it will displaykey1:val1
. - Sort the database — records will be reordered lexicographically.
- Display all records — prints the contents of the database.
- Exit.
This program emulates a rudimentary database system using simple file operations and input/output handling in C.
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node