0% found this document useful (0 votes)
29 views

DSC PG 1b

The program defines functions to create, read, and display a weekly calendar. The create function prompts the user to input details for each day and saves it to a file. The read function loads the saved data from the file. The display function prints the calendar with the name, date, and activity for each day. The main function gets a menu choice to call either the create or read function, then displays the calendar.

Uploaded by

bhumikap050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

DSC PG 1b

The program defines functions to create, read, and display a weekly calendar. The create function prompts the user to input details for each day and saves it to a file. The read function loads the saved data from the file. The display function prints the calendar with the name, date, and activity for each day. The main function gets a menu choice to call either the create or read function, then displays the calendar.

Uploaded by

bhumikap050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Develop a Program in C for the following: Write functions create(), read() and display(); to create the

calendar, to read the data from the keyboard and to print weeks activity details report on screen.

#include <stdio.h>

#include <string.h>

struct Day

char name[20];

int day;

int month;

int year;

char activity[100];

};

void create(struct Day calendar[7])

int i;

for (i = 0; i < 7; i++)

printf("Enter details for %s:\n", calendar[i].name);

printf("Date (DD/MM/YYYY): ");

scanf("%d/%d/%d", &calendar[i].day, &calendar[i].month, &calendar[i].year);

printf("Activity: ");

scanf(" %[^\n]", calendar[i].activity);

// After creating, save to file

FILE *file = fopen("calendar.txt", "w");

if (file == NULL)

printf("Error opening the file.\n");


return;

for (i = 0; i < 7; i++)

fprintf(file, "%s %d/%d/%d %s\n", calendar[i].name, calendar[i].day, calendar[i].month,


calendar[i].year, calendar[i].activity);

fclose(file);

void read(struct Day calendar[7])

int i;

FILE *file = fopen("calendar.txt", "r");

if (file == NULL)

printf("Error opening the file.\n");

return;

for (i = 0; i < 7; i++)

fscanf(file, "%s %d/%d/%d %[^\n]", calendar[i].name, &calendar[i].day, &calendar[i].month,


&calendar[i].year, calendar[i].activity);

fclose(file);

void display(struct Day calendar[7])

int i;

printf("Calendar for the week:\n");


for (i = 0; i < 7; i++)

printf("%s (Date: %02d/%02d/%d): %s\n", calendar[i].name, calendar[i].day, calendar[i].month,


calendar[i].year, calendar[i].activity);

int main()

struct Day calendar[7];

strcpy(calendar[0].name, "Monday");

strcpy(calendar[1].name, "Tuesday");

strcpy(calendar[2].name, "Wednesday");

strcpy(calendar[3].name, "Thursday");

strcpy(calendar[4].name, "Friday");

strcpy(calendar[5].name, "Saturday");

strcpy(calendar[6].name, "Sunday");

int choice;

printf("1. Create Calendar\n");

printf("2. Read Calendar from file\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

create(calendar);

break;

case 2:

read(calendar);

break;

default:

printf("Invalid choice.\n");
return 1;

display(calendar);

return 0;

You might also like