This code implements a simple document management system in C....

September 2, 2025 at 04:20 PM

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <dirent.h> #include <errno.h> #define Cartella "TEST/" // Funzione per creare un documento void creaDocumento() { // Prendo in input il nome del documento e il suo contenuto char nome[64], contenuto[512]; printf("Nome documento: "); fgets(nome, sizeof(nome), stdin); nome[strcspn(nome, "\n")] = 0; printf("Contenuto: "); fgets(contenuto, sizeof(contenuto), stdin); contenuto[strcspn(contenuto, "\n")] = 0; char doc[128]; snprintf(doc, sizeof(doc), "%s%s.txt", Cartella, nome); FILE *file = fopen(doc, "w"); if (!file) { perror("Errore nella creazione del documento"); return; } fwrite(contenuto, sizeof(char), strlen(contenuto), file); fclose(file); printf("Documento creato.\n"); } // Funzione per mostrare i documenti nella cartella void listaDocumenti() { DIR *dir = opendir(Cartella); if (!dir) { perror("Errore nell'aprire la cartella"); return; } struct dirent *entry; printf("Lista documenti:\n"); while ((entry = readdir(dir)) != NULL) { if (entry->d_type == DT_REG) { printf("%s\n", entry->d_name); } } closedir(dir); } // Funzione per leggere il contenuto di un documento void leggiDocumento() { char nome[64]; printf("Nome documento da leggere: "); fgets(nome, sizeof(nome), stdin); nome[strcspn(nome, "\n")] = 0; char doc[128]; snprintf(doc, sizeof(doc), "%s%s.txt", Cartella, nome); FILE *file = fopen(doc, "r"); if (!file) { perror("Errore nell'aprire il documento"); return; } char buffer[512]; printf("Contenuto documento:\n"); while (fgets(buffer, sizeof(buffer), file)) { printf("%s", buffer); } fclose(file); } // Funzione per eliminare un documento void eliminaDocumento() { char nome[64]; printf("Nome documento da eliminare: "); fgets(nome, sizeof(nome), stdin); nome[strcspn(nome, "\n")] = 0; char doc[128]; snprintf(doc, sizeof(doc), "%s%s.txt", Cartella, nome); if (remove(doc) == 0) { printf("Documento eliminato.\n"); } else { perror("Errore nell'eliminare il documento"); } } int main() { int scelta; mkdir(Cartella, 0777); // Avvio una interazione persistente fra programma e utente per la gestione dei documenti do { printf("\nGestore Documenti\n"); printf("1) Crea Documento\n"); printf("2) Lista Documento\n"); printf("3) Leggi Documento\n"); printf("4) Elimina Documento\n"); printf("5) Finisci Gestione Documenti\n"); printf("Scelta: "); scanf("%d", &scelta); getchar(); switch (scelta) { case 1: creaDocumento(); break; case 2: listaDocumenti(); break; case 3: leggiDocumento(); break; case 4: eliminaDocumento(); break; case 5: printf("Chiusura programma.\n"); break; default: printf("Opzione non valida.\n"); } } while (scelta != 5); return 0; }

This code implements a simple document management system in C. Here's a breakdown of its functionality:

  1. Purpose:
    • The program allows users to create, list, read, and delete text documents stored in a specific folder on the local filesystem (TEST/).
    • The folder TEST/ is created automatically when the program starts (if it doesn't already exist).

  1. Key Components:

    • Macros and Includes:

      • It includes standard libraries like stdio.h, stdlib.h, string.h, sys/stat.h (for folder creation), dirent.h (for directory manipulation), and errno.h (for handling errors).
      • The folder name is defined with the macro #define Cartella "TEST/".
    • Functions:

      1. creaDocumento():

        • Prompts the user for a document name and content.
        • Saves the content into a .txt file under the TEST/ directory.
        • If the file cannot be created, an error is displayed.
      2. listaDocumenti():

        • Opens the TEST/ folder.
        • Lists all regular files (documents) stored in the folder.
      3. leggiDocumento():

        • Prompts the user for the name of a document to read.
        • Displays the content of the selected .txt file.
        • Errors are reported if the file cannot be opened.
      4. eliminaDocumento():

        • Prompts the user for the name of a document to delete.
        • Deletes the specified .txt file from the TEST/ folder.
        • Reports success or an error (if the file doesn't exist or cannot be deleted).

  1. main() Function:
    • Creates the TEST/ folder (using mkdir()) with permissions 0777.
    • Enters a loop where the user can select one of the following options:
      1. Create a document.
      2. List all documents.
      3. Read a document.
      4. Delete a document.
      5. Exit the program.
    • The user's choice is handled by a switch statement. For invalid input, an appropriate message is displayed.
    • The loop ends only when the user selects 5 to exit.

  1. How It Works:
    • The program facilitates basic file operations in a pre-defined directory (TEST/).
    • Users interact via a menu-driven interface in the terminal.
    • All document filenames include the .txt extension and are stored relative to the TEST/ folder.
    • The program ensures proper input handling (e.g., stripping newline characters) and error messaging when operations fail.

  1. Example:
    • On running the program:
      Gestore Documenti
      1) Crea Documento
      2) Lista Documento
      3) Leggi Documento
      4) Elimina Documento
      5) Finisci Gestione Documenti
      Scelta:
      
    • If the user selects 1 (create a document):
      Nome documento: Note1
      Contenuto: This is a test document.
      Documento creato.
      
    • If the user selects 2 (list documents):
      Lista documenti:
      Note1.txt
      
    • If the user selects 3 (read a document), and inputs Note1:
      Contenuto documento:
      This is a test document.
      
    • If the user selects 4 (delete a document), and inputs Note1:
      Documento eliminato.
      
    • If the user selects 5, the program exits.

  1. Edge Cases:
    • The folder TEST/ must exist (created if missing).
    • The input for document names/content should not exceed the preallocated buffer sizes.
    • File operations handle errors gracefully with error messages.

Summary:

This code is a menu-driven document management system that allows basic CRUD operations (Create, Read, Update/Delete) on text files stored in a designated directory.

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