This code implements a simple text-based database manager in C....
This code implements a simple text-based database manager in C. It allows users to interact with a file-based database containing key-value pairs (in the format key:value
). The database is stored in a text file (TEST/database.txt
). Here's what the program does:
High-level Summary:
-
Key Features:
- Add a
key:value
pair to the database. - Search for a record by key.
- Sort the database alphabetically by key.
- Display all database contents.
- Exit the program.
- Add a
-
Menu Interaction: The program operates through a persistent menu that lets the user select the desired operation.
-
Database File: The database file stores each
key:value
pair on a new line. If the file doesn't exist, it is automatically created.
Detailed Features and Code Behavior:
1. Validation (inputValido
):
The inputValido
function uses regular expressions to check if the input key is valid:
- A valid key can only contain alphanumeric characters (
[a-zA-Z0-9]
). - If the key doesn't match this pattern, the function returns
0
.
2. Check for Duplicates (fiaPresente
):
The fiaPresente
function checks if a given key is already present in the database file:
- The file is read line by line.
- Each line is parsed to extract its key.
- If the key matches the provided one, it indicates the key already exists.
3. Add Records (aggiungiRecord
):
- Prompts the user to input a key and value.
- Validates the key using
inputValido
. - Checks the database using
fiaPresente
to ensure the key is not already in use. - If the input is valid and unique, it appends the new
key:value
pair to the database file.
4. Search Records (cercaRecord
):
- Prompts the user for a key and searches for it in the database file.
- Reads the file line by line, extracting the key from each line.
- If the specified key is found, the corresponding record is printed; otherwise, it prints "No record found."
5. Sort Database (ordinaDatabase
):
- Reads all the lines (records) from the database file into memory.
- Sorts the records alphabetically by key using
qsort
. - Writes the sorted records back into the database file, overwriting its contents.
6. Display Database (mostraDatabase
):
- Opens the database file and displays its contents line by line.
- Ensures that the entire database file is presented in the console.
7. Menu System (main
):
- Implements a persistent menu using a
do-while
loop. - Options:
- Add a record.
- Search for a record.
- Sort the database.
- Display the database.
- Exit the program.
- Users enter their choice (validated) and the corresponding function is invoked.
8. Error Handling:
- Ensures that the database file is properly opened/closed.
- Handles potential errors such as missing files or failed memory allocation during sorting.
Issues in the Code:
- Missing headers: The function
inputValido
uses regex-related functionality, but<regex.h>
is not included. - Typographical Errors:
- The function
fiaPresente
incorrectly declaresFile
instead ofFILE
for file handling. - Missing closing semicolons in a few places (e.g.,
printf("Chiave gia presente.\n")
infiaPresente
). - Function
giaPresente
is called inaggiungiRecord
instead of the correctfiaPresente
function.
- The function
- Bug in Searching (
cercaRecord
):- It attempts to compare the entire line (
riga
) with the key (chiave
). - Should extract the key portion from the file line using
sscanf
and compare it.
- It attempts to compare the entire line (
- Potential Overflow:
- Fixed-size buffers may lead to overflow if inputs exceed their sizes.
Correct Usage Scenario:
The code is designed to maintain and manipulate a simple file-based key-value store. Here's an example workflow:
- Run the program, initializing the file if necessary.
- Use the menu to:
- Add new
key:value
pairs. - Search by specifying a key.
- Sort and display records.
- Add new
This provides a functional but basic interface for managing a minimalistic textual database in C.