DEV Community

Cover image for Command-Line Planner in C (with Saving and Loading)

Posted on

Command-Line Planner in C (with Saving and Loading)


A calendar or planner is a great tool to help keep track of appointments, events, and tasks. In this tutorial, I'll guide you through the process of creating a simple command-line calendar and planner using the C programming language. Not only will you be able to add and view events, but we'll also implement the ability to save your events to a file and load them back when you start the program again. Let's get started!


  • Basic knowledge of the C programming language.
  • A C compiler installed on your system (e.g., GCC) or a Repl.
  • A text editor or an Integrated Development Environment (IDE) for writing C code.


Our event calender will following features:

  • Add Event: We can add events by specifying a date (in the format YYYY-MM-DD) and a description for the event.
  • View Events for a Date: We can view all events scheduled for a specific date.
  • View All Events: We can see a list of all the events in the calendar.
  • Save Events to File: Events can be saved to a text file for persistence.
  • Load Events from File: When the program starts, it will attempt to load previously saved events from the file.
  • Exit: We can exit the program, and it will save the events before exiting.

Step 1: Define the Event Structure

We will start by defining a structure to represent an event. Open your text editor and create a new C source file (e.g., 'calendar.c'). Add the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Event {
    char date[12];   // Date in format YYYY-MM-DD
    char description[100];
} Event;
Enter fullscreen mode Exit fullscreen mode

Step 2: Implement Functions

// Function to add an event to the calendar
void addEvent(Event *calendar, int *eventCount) {
    if (*eventCount >= 100) {
        printf("Calendar is full. Cannot add more events.\n");

    Event newEvent;
    printf("Enter event date (YYYY-MM-DD): ");
    printf("Enter event description: ");
    getchar();  // Consume newline left in the buffer
    fgets(newEvent.description, sizeof(newEvent.description), stdin);

    calendar[*eventCount] = newEvent;
    printf("Event added successfully!\n");

// Function to view events for a specific date
void viewEvents(Event *calendar, int eventCount, char *date) {
    printf("Events for %s:\n", date);
    for (int i = 0; i < eventCount; i++) {
        if (strcmp(calendar[i].date, date) == 0) {
            printf("- %s", calendar[i].description);

// Function to view all events
void viewAllEvents(Event *calendar, int eventCount) {
    printf("All Events:\n");
    for (int i = 0; i < eventCount; i++) {
        printf("Date: %s\n", calendar[i].date);
        printf("Description: %s", calendar[i].description);

// Function to save events to a text file
void saveEventsToFile(Event *calendar, int eventCount, const char *filename) {
    FILE *file = fopen(filename, "w");
    if (file == NULL) {
        printf("Error opening file for saving.\n");

    for (int i = 0; i < eventCount; i++) {
        fprintf(file, "%s\n", calendar[i].date);
        fprintf(file, "%s", calendar[i].description);


// Function to load events from a text file
int loadEventsFromFile(Event *calendar, int *eventCount, const char *filename) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        return 0;  // File doesn't exist or cannot be opened

    while (fscanf(file, "%s", calendar[*eventCount].date) != EOF) {
        fgetc(file);  // Consume newline
        fgets(calendar[*eventCount].description, sizeof(calendar[*eventCount].description), file);

    return 1;  // File successfully loaded
Enter fullscreen mode Exit fullscreen mode

Step 3: Implement the Main Function

In the main function, the program to load events from a file (creating a new file if not found), and it will also save events before exiting:

int main() {
    Event calendar[100];  // Array to store events
    int eventCount = 0;   // Number of events in the calendar

    // Try loading events from a file, create a new file if not found
    if (!loadEventsFromFile(calendar, &eventCount, "events.txt")) {
        printf("No saved events found. Creating a new calendar.\n");

    while (1) {
        printf("1. Add Event\n");
        printf("2. View Events for a Date\n");
        printf("3. View All Events\n");
        printf("4. Save Events to File\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");

        int choice;
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addEvent(calendar, &eventCount);
            case 2: {
                char date[12];
                printf("Enter date (YYYY-MM-DD): ");
                scanf("%s", date);
                viewEvents(calendar, eventCount, date);
            case 3:
                viewAllEvents(calendar, eventCount);
            case 4:
                saveEventsToFile(calendar, eventCount, "events.txt");
                printf("Events saved to file.\n");
            case 5:
                // Save events before exiting
                saveEventsToFile(calendar, eventCount, "events.txt");
                printf("Invalid choice. Try again.\n");
    return 0;
Enter fullscreen mode Exit fullscreen mode

Step 4: Compile and Run

You can run the following program through GCC compiler or online coding IDE.

You can run the following program here :

Thank you so much for reading.
Follow and Like for more.

  • You can also follow me on LinkedIn, I think building connections is better

Top comments (0)