Welcome to the Diary Application tutorial! In this program, you’ll learn how to create a simple diary application that allows you to write and save your daily entries.

Introduction

A diary application allows users to record their thoughts, feelings, and experiences in a structured format. By using this simple program, you can store your daily thoughts securely and review them whenever you need. This tutorial will guide you through creating a basic C program that enables you to write, save, and load diary entries.

Objective

The objective of this program is to create a diary application where users can:

  • Write a daily entry.
  • Save the entry in a text file.
  • View previous diary entries.

Code

#include 
#include 

void write_entry() {
    FILE *file;
    char entry[1000];
    
    // Open the file in append mode to add new entries
    file = fopen("diary.txt", "a");
    
    if (file == NULL) {
        printf("Error opening the file.\n");
        return;
    }
    
    printf("Enter your diary entry: \n");
    getchar();  // To consume newline character left in the buffer
    fgets(entry, sizeof(entry), stdin);
    
    // Write the entry to the file
    fprintf(file, "Entry: \n%s\n\n", entry);
    printf("Your entry has been saved successfully.\n");
    
    fclose(file);
}

void view_entries() {
    FILE *file;
    char ch;
    
    // Open the file in read mode to view saved entries
    file = fopen("diary.txt", "r");
    
    if (file == NULL) {
        printf("No entries found!\n");
        return;
    }
    
    printf("Your previous diary entries: \n\n");
    
    // Read and print the content of the file
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }
    
    fclose(file);
}

int main() {
    int choice;
    
    while (1) {
        printf("\n--- Diary Application ---\n");
        printf("1. Write a new entry\n");
        printf("2. View all entries\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        
        switch(choice) {
            case 1:
                write_entry();
                break;
            case 2:
                view_entries();
                break;
            case 3:
                printf("Exiting Diary Application. Goodbye!\n");
                exit(0);
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }
    
    return 0;
}

Explanation of the Program Structure

The program consists of the following functions:

  • write_entry(): This function allows the user to input a new diary entry and saves it in a text file named “diary.txt”. The fgets() function is used to capture user input including spaces, and the entry is written into the file using fprintf().
  • view_entries(): This function allows the user to view all previously written diary entries stored in the “diary.txt” file. The fgetc() function is used to read each character of the file and display it to the user.
  • main(): The main function acts as the interface to the program. It provides the user with a menu to either write a new entry, view previous entries, or exit the application. The program runs in an infinite loop until the user chooses to exit.

How to Run the Program

  1. Copy the code into a C programming environment or a text file (e.g., diary.c).
  2. Compile the program using a C compiler. For example, if you are using GCC, run the following command:
    gcc diary.c -o diary
  3. Run the compiled program using:
    ./diary
  4. The program will display a menu with options to write a new entry, view previous entries, or exit.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)