This C code implements a program to manage and update...
This C code implements a program to manage and update logs, storing information about either customers (Cliente
) or orders (Ordine
). Here's a breakdown of what the code does:
1. Structures Defined
-
Cliente
:- Represents a customer, with fields for a name (
nome
), surname (cognome
), email, and loyalty points (puntiFedelta
).
- Represents a customer, with fields for a name (
-
Ordine
:- Represents an order, with fields for a customer ID (
cid
), product quantity (quantita
), price (prezzo
), and product name (prodotto
).
- Represents an order, with fields for a customer ID (
2. Functionality
gestisciLog(void* log)
- Interprets raw log data as an
Ordine
object, calculates the total cost (quantita
*prezzo
), and prints the order details. - This function is generic and always interprets the log as an order, even if it gets passed
Cliente
data (may lead to undefined behavior when used incorrectly).
aggiornaLog
- This is the main function used for updating log files (either customer log
clienti.txt
or order logordini.txt
). - Steps:
- Opens the log file (
clienti.txt
for customers orordini.txt
for orders) in read mode. - Reads log data into memory, storing it as either
Cliente
orOrdine
objects. - Searches for a record that matches the provided key (
chiave
), which is the email for customers or the order ID (cid
) for orders.- If a customer (
isCliente == 1
): Updates the customer's loyalty points. - If an order (
isCliente == 0
): Updates the order's product price.
- If a customer (
- If a matching record is found and updated, writes the updated data back to the log file in write mode.
- If no record matches the key, prints a message stating it wasn't found.
- Opens the log file (
Note: The function dynamically allocates memory (malloc
), but there is no proper cleanup (free
), which may cause memory leaks.
main
- Requests the user to choose whether to update a customer ("Cliente") or order ("Ordine").
- Based on the user's choice:
- If "Cliente":
- Asks for the customer's email and the points to award, then calls
aggiornaLog
withisCliente = 1
.
- Asks for the customer's email and the points to award, then calls
- If "Ordine":
- Asks for the order ID (
cid
) and the new price, then callsaggiornaLog
withisCliente = 0
.
- Asks for the order ID (
- If the input doesn't match either option, the program prints that the choice is invalid and exits.
- If "Cliente":
Output is written to clienti.txt
(for customers) or ordini.txt
(for orders) based on the operation.
Usage
- Manage Customers:
- Reads from
clienti.txt
with the formatnome cognome email puntiFedelta
. - Updates the loyalty points for the specified customer based on their email address.
- Reads from
- Manage Orders:
- Reads from
ordini.txt
with the formatcid prodotto quantita prezzo
. - Updates the price of the product in the specified order, identified by its
cid
.
- Reads from
Example Workflow
File: clienti.txt
Mario Rossi mario.rossi@example.com 150.0
Luigi Bianchi luigi.bianchi@example.com 200.0
File: ordini.txt
123 pasta 5 3.50
456 latte 2 1.80
-
User selects "Cliente".
- Enters email:
mario.rossi@example.com
. - Enters points to add:
50
. - Result: The updated
clienti.txt
will be:Mario Rossi mario.rossi@example.com 200.0 Luigi Bianchi luigi.bianchi@example.com 200.0
- Enters email:
-
User selects "Ordine".
- Enters CID:
123
. - Enters new price:
4.5
. - Result: The updated
ordini.txt
will be:123 pasta 5 4.50 456 latte 2 1.80
- Enters CID:
Issues and Observations
-
Memory Management:
- Dynamic memory is used (
malloc
), but it is not properly freed, leading to memory leaks.
- Dynamic memory is used (
-
Error Handling:
- If the logs are not correctly formatted or the file is missing, the program may fail or behave unexpectedly.
-
gestisciLog
Misuse:- The
gestisciLog
function assumes all logs areOrdine
structures, even ifCliente
data is passed. This could lead to undefined behavior or incorrect interpretation.
- The
-
File Overwrite:
- The entire file is rewritten even if only one record is updated.
Summary
This code allows the user to update customer loyalty points or order prices in separate log files (clienti.txt
and ordini.txt
). It parses the files, modifies relevant entries, and saves the updates. Improvements are needed for memory management, error handling, and the generic log interpretation approach.