Lab 2: File I/O | CMSC 240 Software Systems Development - Spring 2024

Lab 2: File I/O

Personal Diary Management System

Objective

Understand and practice file handling in C++ using ifstream and ofstream classes. Develop a basic Personal Diary Management System to create, read, and update diary entries.

Problem Statement

Design a command-line interface (CLI) Personal Diary Management System that has the following features:

Instructions:

Use the fstream header. Use ifstream to read from files and ofstream to write to files. Create appropriate error handling for file operations, such as when a file doesn’t exist or there’s an error during reading/writing. Keep the user interface simple, text-based, and intuitive.

The code for this exercise is in the lab2 repository. Read the description below, and then enter your code in the diary.cpp file where it says:

// TODO: Write your code here.

To append to a file. You can open the file with the ofstream::app option, like this:

    ofstream diaryEntryFileStream;
    diaryEntryFileStream.open(filename, ofstream::app);

When you finish and test your code, write in the README.md file how your code works.

Here is some sample output:

$ ./diary
Welcome to the Personal Diary Management System 1.0

Choose an operation:
(x) : Exit
(n) : Create a New Entry
(r) : Read an Entry
(a) : Append to an Entry
n
Please enter the diary entry date in the format YYYY_MM_DD: 2023_09_08
Type your diary text for 2023_09_08 (Enter END on a new line when done.):
Dear Diary,
Today I wrote a diary entry program.
END

Choose an operation:
(x) : Exit
(n) : Create a New Entry
(r) : Read an Entry
(a) : Append to an Entry
a
Please enter the diary entry date in the format YYYY_MM_DD: 2023_09_08
Type your diary text for 2023_09_08 (Enter END on a new line when done.):
The program worked!
END

Choose an operation:
(x) : Exit
(n) : Create a New Entry
(r) : Read an Entry
(a) : Append to an Entry
r
Please enter the diary entry date in the format YYYY_MM_DD: 2023_09_08

Dear Diary,
Today I wrote a diary entry program.

The program worked!

Choose an operation:
(x) : Exit
(n) : Create a New Entry
(r) : Read an Entry
(a) : Append to an Entry
r
Please enter the diary entry date in the format YYYY_MM_DD: 2025_01_01
Could not find diary entry for 2025_01_01

Choose an operation:
(x) : Exit
(n) : Create a New Entry
(r) : Read an Entry
(a) : Append to an Entry
x