The code implements a simple user management system in C,...

September 3, 2025 at 11:43 PM

#include <stdio.h> #include <stdlib.h> #include <string.h> #define DBUTENTI "TEST/utenti.txt" // Funzione per aggiungere un utente void aggiungiUtente() { char nomeUtente[64], password[64], riga[130]; printf("Nome Utente: "); fgets(nomeUtente, sizeof(nomeUtente), stdin); nomeUtente[strcspn(nomeUtente, "\n")] = 0; printf("Password: "); fgets(password, sizeof(password), stdin); password[strcspn(password, "\n")] = 0; FILE *file = fopen(DBUTENTI, "r+"); if (!file) { file = fopen(DBUTENTI, "w+"); if (!file) { printf("Errore apertura file.\n"); return; } } // Controllo se utente esiste già while (fgets(riga, sizeof(riga), file)) { char temp[64]; sscanf(riga, "%[^:]", temp); if (strcmp(temp, nomeUtente) == 0) { printf("Nome Utente già in utilizzo!\n"); fclose(file); return; } } // Aggiungo utente fseek(file, 0, SEEK_END); fprintf(file, "%s:%s\n", nomeUtente, password); fclose(file); printf("Utente aggiunto con successo.\n"); } // Funzione per cercare un utente void cercaUtente() { char nomeUtente[64], riga[130]; int trovato = 0; printf("Nome utente da cercare: "); fgets(nomeUtente, sizeof(nomeUtente), stdin); nomeUtente[strcspn(nomeUtente, "\n")] = 0; FILE *file = fopen(DBUTENTI, "r"); if (!file) { printf("Database vuoto o non accessibile.\n"); return; } while (fgets(riga, sizeof(riga), file)) { char temp[64]; sscanf(riga, "%[^:]", temp); if (strcmp(temp, nomeUtente) == 0) { trovato = 1; break; } } fclose(file); if (trovato) printf("Utente '%s' è presente.\n", nomeUtente); else printf("Utente '%s' NON è presente.\n", nomeUtente); } // Funzione per cambiare password void cambiaPassword() { char nomeUtente[64], nuovaPassword[64]; char riga[130]; int trovato = 0; printf("Nome utente: "); fgets(nomeUtente, sizeof(nomeUtente), stdin); nomeUtente[strcspn(nomeUtente, "\n")] = 0; printf("Nuova password: "); fgets(nuovaPassword, sizeof(nuovaPassword), stdin); nuovaPassword[strcspn(nuovaPassword, "\n")] = 0; FILE *file = fopen(DBUTENTI, "r"); if (!file) { printf("Database vuoto o non accessibile.\n"); return; } // Carico tutto in memoria char *righe[1024]; int count = 0; while (fgets(riga, sizeof(riga), file)) { righe[count] = strdup(riga); if (!righe[count]) { perror("malloc"); fclose(file); return; } count++; } fclose(file); // Aggiorno la password se trovo l'utente for (int i = 0; i < count; i++) { char temp[64]; sscanf(righe[i], "%[^:]", temp); if (strcmp(temp, nomeUtente) == 0) { snprintf(riga, sizeof(riga), "%s:%s\n", nomeUtente, nuovaPassword); free(righe[i]); righe[i] = strdup(riga); trovato = 1; break; } } if (!trovato) { printf("Utente non trovato.\n"); for (int i = 0; i < count; i++) free(righe[i]); return; } // Riscrivo tutto il file file = fopen(DBUTENTI, "w"); if (!file) { perror("Errore apertura file scrittura"); for (int i = 0; i < count; i++) free(righe[i]); return; } for (int i = 0; i < count; i++) { fputs(righe[i], file); free(righe[i]); } fclose(file); printf("Password aggiornata con successo.\n"); } int main() { int scelta; do { printf("\nGestore Utenti\n"); printf("1) Aggiungi Utente\n"); printf("2) Cerca Utente\n"); printf("3) Cambia Password\n"); printf("4) Esci\n"); printf("Scelta: "); scanf("%d", &scelta); getchar(); switch (scelta) { case 1: aggiungiUtente(); break; case 2: cercaUtente(); break; case 3: cambiaPassword(); break; case 4: printf("Chiusura.\n"); break; default: printf("Scelta non valida.\n"); } } while (scelta != 4); return 0; }

The code implements a simple user management system in C, providing functionalities to add users, search for users, and change user passwords, storing the data in a text file. Here's a breakdown of what it does:

Structure of the File

  • Users and their passwords are stored in a file defined by the macro DBUTENTI (#define DBUTENTI "TEST/utenti.txt"). Each line in the file is structured as username:password.

Key Functionalities

  1. Adding a User (aggiungiUtente)
    • Asks the user to input a username and password.
    • Checks if the file DBUTENTI exists or creates it if it doesn't.
    • Reads the file to check if a user with the entered username already exists.
      • If the username exists, it prints: Nome Utente già in utilizzo! (Username already in use).
    • If the username is unique, it appends the username:password combination at the end of the file.
    • Provides feedback to indicate success or failure.

  1. Searching for a User (cercaUtente)
    • Asks for the username to search for.
    • Opens the file and reads all lines, checking if the username exists in the file.
    • If found, it prints: Utente '<username>' è presente. (User '<username>' is present).
    • If not found, it prints: Utente '<username>' NON è presente. (User '<username>' is NOT present).
    • Handles cases where the file is missing or inaccessible by printing an appropriate message.

  1. Changing a User's Password (cambiaPassword)
    • Prompts for a username and a new password.
    • Reads all the lines of the file into memory.
    • Searches for the username in memory and updates its password if found.
    • If the username is not found, it prints: Utente non trovato. (User not found).
    • If the username is found:
      • Updates the password associated with that username.
      • Overwrites the file with the updated data.
      • Prints: Password aggiornata con successo. (Password updated successfully).
    • Frees any dynamically allocated memory used for handling lines.

  1. Main Functionality (main)
    • Provides a menu-driven interface to interact with the user:
      1. Aggiungi Utente (Add User) calls aggiungiUtente.
      2. Cerca Utente (Search User) calls cercaUtente.
      3. Cambia Password (Change Password) calls cambiaPassword.
      4. Exit option (prints "Chiusura." and terminates the program).
    • Ensures user input is managed properly (e.g., handling the extra newline after entering the choice).

Notes

  • File Structure: The file is simple, storing each user on a single line as username:password.
  • Error Handling: The program checks if the file can be opened and ensures proper memory management in functions like cambiaPassword.
  • Security Concerns:
    • Passwords are stored in plaintext, which is a significant security risk in real-world scenarios.
    • Memory is allocated for storing file content in cambiaPassword, which could lead to inefficiency for large files.
  • Interaction: Users interact with the program through a basic console interface.

Summary

The code is a basic user management system, providing operations for adding users, searching users, and updating passwords, all of which are stored in a simple text file.

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