简体   繁体   中英

C - avoiding a newline when using fprintf with time.h

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

ctime(3) - Linux man page says:

The call ctime(t) is equivalent to asctime(localtime(t)) . It converts the calendar time t into a null-terminated string of the form
"Wed Jun 30 21:49:08 1993\n"

It looks like adding newline character is included in the specification of ctime() . You should use strftime() to manually format the date and time without newline.

char date[128];
strftime(date, sizeof(date), "%a %b %d %T %Y", localtime(&now));
fprintf(logFilePtr, "%s", date);

As per documentation of ctime , the string has a newline embedded. You need to remove it from consideration for printing.

Since the string returned by ctime has a specified format of constant width, this should accomplish that:

fprintf(logFilePtr, "%.24s ", ctime(&now));

(edit: This is assuming you wish to use ctime in the first place, as you do in the question. There are other, potentially better, options, such as strftime .)

The ctime function always adds '\n' to the end of string . There are many different ways to solve the problem, but the easiest way is to remove this symbol. This can be done, for example, like this (based on the fact that the ctime ( see man ) returns char * , not const char * ):

#include <string.h>
...
char *ctime_line = ctime (&now);
ctime_line[strlen (ctime_line) - 1] = '\0';

and then print this line:

fprintf (logFilePtr, "%s", ctime_line);

PS I do not recommend using a record like this, since the string AAA may contain, for example, the % character, which will be interpreted as a special character.

// bad
fprintf (logFilePtr, logMessagePtr);
// good
fprintf (logFilePtr, "%s", logMessagePtr);

PPS The following three lines

fprintf (logFilePtr, "%s", ctime (&now));
fprintf (logFilePtr, "%s", logMessagePtr);
fprintf (logFilePtr, "\n");

can be combined into one

fprintf (logFilePtr, "%s %s\n", ctime (&now), logMessagePtr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM