I keep getting a new line printed after the current time is written to a log.txt file. In this example I want the time and logMessage printed on the same line. Could you point me in the right direction as to what I'm missing here?
The logWrite function is called from the main function:
strcpy(logMessage, "**********RESTART**********");
logWrite(logMessagePtr);
The logWrite function is:
void logWrite(char * logMessagePtr)
{
/* Initialise time variables for log file. */
int hours, minutes, seconds, day, month, year;
// time_t is an arithmetic time type
time_t now;
// Update current time using time(&now);
time(&now);
FILE * logFilePtr;
logFilePtr = fopen ("C:\\log.txt", "a");
if (logFilePtr != NULL)
{
fprintf(logFilePtr, ("%s", ctime(&now)));
fprintf(logFilePtr, "%s", logMessagePtr);
fprintf(logFilePtr,"\n");
fclose(logFilePtr);
printf("Log file found\n"); //debug
}
else
{
perror("log.txt");
}
}
The log.txt file is sucessfully (created) appended to but I consistently get a new line feed after the time is written to the file.
When I run the program twice in a row for example, the log.txt reads:
Sat May 22 09:16:47 2021
**********RESTART**********
Sat May 22 09:16:48 2021
**********RESTART**********
What I want is:
Sat May 22 09:16:47 2021 **********RESTART**********
Sat May 22 09:16:48 2021 **********RESTART**********
Info on time.h