Open In App

Simple Personal Diary Management System in C

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Features of Diary Management System in C

This simple diary management application will provide the following features:

1. Add Diary Entries

The users will be able to add the entries in the diary in the form of text with a maximum of 500 characters. These entries will be saved inside an external file with their timestamp. A single file will be used for storing entries no matter how many times the program is run so that previous entries can also be viewed in the current run.

2. View Diary Entries

The diary entries will be saved inside an external file with their timestamp and we can retrieve these entries along with their time stamp and print them on the console screen. The view entries functionality will show all the dairy entries present in the file.

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 code output

Main Menu Output

Entering and Viewing Entries

exiting application output

Exiting the Application



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads