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 usingfprintf()
. - 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
- Copy the code into a C programming environment or a text file (e.g., diary.c).
- Compile the program using a C compiler. For example, if you are using GCC, run the following command:
gcc diary.c -o diary
- Run the compiled program using:
./diary
- The program will display a menu with options to write a new entry, view previous entries, or exit.