feat(assign1): add logwriter.c
This commit is contained in:
parent
eb87fd068d
commit
a0392134e2
2 changed files with 40 additions and 0 deletions
39
assign1/c-programs/logwriter.c
Normal file
39
assign1/c-programs/logwriter.c
Normal 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(¤t_time);
|
||||
|
||||
// Write to file with timestamp
|
||||
// fprintf works like printf but writes to a file
|
||||
fprintf(logfile, "[%s] %s", ctime(¤t_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;
|
||||
}
|
||||
|
|
@ -18,4 +18,5 @@ let
|
|||
in {
|
||||
assign1-hello = basic-c "hello";
|
||||
assign1-employee = basic-c "employee";
|
||||
assign1-logwriter = basic-c "logwriter";
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue