feat(assign1): add logwriter.c

This commit is contained in:
Kiana Sheibani 2025-09-04 21:53:15 -04:00
parent eb87fd068d
commit a0392134e2
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 40 additions and 0 deletions

View file

@ -0,0 +1,39 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main() {
FILE *logfile; // Pointer to file structure
char message[100];
time_t current_time;
// Open file for appending ("a" mode)
// "w" would overwrite, "r" would read
logfile = fopen("owltech.log", "a");
// Always check if file opened successfully
if (logfile == NULL) {
printf("Error: Could not open log file\n");
return 1; // Return non-zero to indicate error
}
printf("Enter log message: ");
fgets(message, sizeof(message), stdin);
// Get current time
time(&current_time);
// Write to file with timestamp
// fprintf works like printf but writes to a file
fprintf(logfile, "[%s] %s", ctime(&current_time), message);
// Always close files when done
fclose(logfile);
printf("Log entry saved successfully!\n");
// Display the log file contents
printf("\n--- Current Log Contents ---\n");
system("cat owltech.log");
return 0;
}

View file

@ -18,4 +18,5 @@ let
in {
assign1-hello = basic-c "hello";
assign1-employee = basic-c "employee";
assign1-logwriter = basic-c "logwriter";
}