C Program to Implement Personal Diary Management System

C




// C program to implement a simple personal diary
// application
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
#define MAX_ENTRY_SIZE 500
 
// Function to add a new diary entry
void addEntry()
{
    char entry[MAX_ENTRY_SIZE];
    printf(
        "Enter your diary entry (max 500 characters):\n");
    getchar(); // Consume the newline character left in the
               // buffer
    fgets(entry, sizeof(entry), stdin);
 
    // Get current date and time
    time_t t = time(NULL);
    struct tm* tm_info = localtime(&t);
    char timestamp[20];
    strftime(timestamp, sizeof(timestamp),
             "%Y-%m-%d %H:%M:%S", tm_info);
 
    // Append the entry to the diary file
    FILE* file = fopen("diary.txt", "a");
    if (file != NULL) {
        fprintf(file, "[%s]\n%s\n\n", timestamp, entry);
        fclose(file);
        printf("Entry added successfully!\n");
    }
    else {
        printf("Error: Could not open the diary file.\n");
    }
}
 
// Function to display all diary entries
void viewEntries()
{
    char line[MAX_ENTRY_SIZE];
    FILE* file = fopen("diary.txt", "r");
    if (file != NULL) {
        while (fgets(line, sizeof(line), file) != NULL) {
            printf("%s", line);
        }
        fclose(file);
    }
    else {
        printf("Error: Could not open the diary file.\n");
    }
}
 
// Driver code
int main()
{
    int choice;
 
    // loop that will run till exit is selected.
    do {
        // main dashboard
        printf("\nPersonal Diary Application\n");
        printf("1. Add Diary Entry\n");
        printf("2. View Diary Entries\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
 
        // choice selection
        switch (choice) {
        case 1:
            addEntry();
            break;
        case 2:
            viewEntries();
            break;
        case 3:
            printf("Exiting the diary application. "
                   "Goodbye!\n");
            break;
        default:
            printf("Invalid choice. Please enter a valid "
                   "option.\n");
        }
    } while (choice != 3);
 
    return 0;
}


Output

Main Menu Output

Entering and Viewing Entries

Exiting the Application



Simple Personal Diary Management System in C

Keeping a personal diary has been a timeless practice for a lot of individuals to reflect on their thoughts and experiences. In this article, we will look at a simple personal diary management application in C.

Prerequisite: Basic C Knowledge, File Handling in C, and Time in C.

Similar Reads

Features of Diary Management System in C

This simple diary management application will provide the following features:...

C Program to Implement Personal Diary Management System

C // C program to implement a simple personal diary // application #include #include #include #include   #define MAX_ENTRY_SIZE 500   // Function to add a new diary entry void addEntry() {     char entry[MAX_ENTRY_SIZE];     printf(         "Enter your diary entry (max 500 characters):\n");     getchar(); // Consume the newline character left in the                // buffer     fgets(entry, sizeof(entry), stdin);       // Get current date and time     time_t t = time(NULL);     struct tm* tm_info = localtime(&t);     char timestamp[20];     strftime(timestamp, sizeof(timestamp),              "%Y-%m-%d %H:%M:%S", tm_info);       // Append the entry to the diary file     FILE* file = fopen("diary.txt", "a");     if (file != NULL) {         fprintf(file, "[%s]\n%s\n\n", timestamp, entry);         fclose(file);         printf("Entry added successfully!\n");     }     else {         printf("Error: Could not open the diary file.\n");     } }   // Function to display all diary entries void viewEntries() {     char line[MAX_ENTRY_SIZE];     FILE* file = fopen("diary.txt", "r");     if (file != NULL) {         while (fgets(line, sizeof(line), file) != NULL) {             printf("%s", line);         }         fclose(file);     }     else {         printf("Error: Could not open the diary file.\n");     } }   // Driver code int main() {     int choice;       // loop that will run till exit is selected.     do {         // main dashboard         printf("\nPersonal Diary Application\n");         printf("1. Add Diary Entry\n");         printf("2. View Diary Entries\n");         printf("3. Exit\n");         printf("Enter your choice: ");         scanf("%d", &choice);           // choice selection         switch (choice) {         case 1:             addEntry();             break;         case 2:             viewEntries();             break;         case 3:             printf("Exiting the diary application. "                    "Goodbye!\n");             break;         default:             printf("Invalid choice. Please enter a valid "                    "option.\n");         }     } while (choice != 3);       return 0; }...