diff --git a/assign1/c-programs/logwriter.c b/assign1/c-programs/logwriter.c new file mode 100644 index 0000000..d5e8087 --- /dev/null +++ b/assign1/c-programs/logwriter.c @@ -0,0 +1,39 @@ +#include +#include +#include + +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; +} diff --git a/assign1/default.nix b/assign1/default.nix index f1cd2ca..f9488a8 100644 --- a/assign1/default.nix +++ b/assign1/default.nix @@ -18,4 +18,5 @@ let in { assign1-hello = basic-c "hello"; assign1-employee = basic-c "employee"; + assign1-logwriter = basic-c "logwriter"; }