|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <time.h> |
| 5 | + |
| 6 | +#define MAX_ENTRY_SIZE 1000 |
| 7 | + |
| 8 | +void addEntry() |
| 9 | +{ |
| 10 | + char entry[MAX_ENTRY_SIZE]; |
| 11 | + printf( |
| 12 | + "Please enter your diary entry (max 1000 characters):\n"); |
| 13 | + getchar(); |
| 14 | + fgets(entry, sizeof(entry), stdin); |
| 15 | + |
| 16 | + time_t t = time(NULL); |
| 17 | + struct tm* tm_info = localtime(&t); |
| 18 | + char timestamp[20]; |
| 19 | + strftime(timestamp, sizeof(timestamp), |
| 20 | + "%Y-%m-%d %H:%M:%S", tm_info); |
| 21 | + |
| 22 | + FILE* file = fopen("Diary.txt", "a"); |
| 23 | + if (file != NULL) { |
| 24 | + fprintf(file, "[%s]\n%s\n\n", timestamp, entry); |
| 25 | + fclose(file); |
| 26 | + printf("Entry added successfully.\n"); |
| 27 | + } |
| 28 | + else { |
| 29 | + printf("Error: Could not open diary file.\n"); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +void viewEntries() |
| 34 | +{ |
| 35 | + char line[MAX_ENTRY_SIZE]; |
| 36 | + FILE* file = fopen("Diary.txt", "r"); |
| 37 | + if (file != NULL) { |
| 38 | + while (fgets(line, sizeof(line), file) != NULL) { |
| 39 | + printf("%s", line); |
| 40 | + } |
| 41 | + fclose(file); |
| 42 | + } |
| 43 | + else { |
| 44 | + printf("Error: Could not open diary file.\n"); |
| 45 | + } |
| 46 | +} |
| 47 | +int main() |
| 48 | +{ |
| 49 | + int choice; |
| 50 | + do { |
| 51 | + printf("\nPersonal Diary App\n"); |
| 52 | + printf("1. Add diary entry\n"); |
| 53 | + printf("2. View diary entries\n"); |
| 54 | + printf("3. Exit\n"); |
| 55 | + printf("Enter option: "); |
| 56 | + scanf("%d", &choice); |
| 57 | + |
| 58 | + switch (choice) { |
| 59 | + case 1: |
| 60 | + addEntry(); |
| 61 | + break; |
| 62 | + case 2: |
| 63 | + viewEntries(); |
| 64 | + break; |
| 65 | + case 3: |
| 66 | + printf("Exiting diary application..."); |
| 67 | + break; |
| 68 | + default: |
| 69 | + printf("Invalid input. Please enter a valid option."); |
| 70 | + } |
| 71 | + } while (choice != 3); |
| 72 | + return 0; |
| 73 | +} |
0 commit comments