In: Computer Science
In C Programming Language
Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL".
Please screenshot the results.
//do comment if any problem arises
//code
#include <time.h>
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
//current time
time_t time_now = time(NULL);
//convert to tm format using localtime function
struct tm *current_time = localtime(&time_now);
//this variable stores Day Time Date of current time
char Time[100];
//end index is returned by strftime to appen null at end
//A is current day,I is for hour, M is for minutes, p is for am or
pm, x is for date
size_t end_index = strftime(Time, 100, "%A %I:%M%p %x",
current_time);
//append null at end
Time[end_index] = '\0';
//open log file log.txt
FILE *log_file = fopen("log.txt", "a");
//if unable to open log file
if (log_file == NULL)
{
printf("Can't open log.txt");
exit(0);
}
//appen a newline to file
fprintf(log_file, "\n");
//write time to file
fprintf(log_file, Time);
//write successful
fprintf(log_file, " SUCCESSFUL");
return 0;
}
Output: