简体   繁体   中英

How can I print the current date and time and a string to one line in a file in C?

I have a log file in my C program which I am trying to make entries to every time the user executes a query. It works, apart from it prints the date and time to one line and the activity string to the line below. I need it to print the whole entry to one line. I have tried everything and don't understand why it won't work. I think it is something to do with the time_string. Please can someone help? Code is shown below;

/*
 * This function writes a log to the log file.
 */
 void write_log(char *activity) {
     FILE *lf;
     time_t current_time;
     char *time_string;

     current_time = time(NULL);
     time_string = ctime(&current_time);
     strcat(time_string, activity);
     lf = fopen("logs.txt", "a+");
     fprintf(lf, "%s\n", activity);
     fclose(lf);
 }

The function is called in the main and is passed a string literal for the activity.

From man ctime :

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"

so a \\n character is included in the resulting string. You must remove the new line character:

char* nl = strrchr(time_string, '\n');
if (nl) *nl = '\0';

Also worth noting, from the same linked reference page:

The return value points to a statically allocated string which might be overwritten by subsequent calls to any of the date and time functions.

This is important for the reason stated and it is unknown how large that buffer is so using it as the target in a strcat() is unsafe due to possible buffer overrun. Instead of performing a strcat() remove the new line character and perform two writes to the file; one for the time_string and one for the activity\\n .

The string returned by ctime ends with a newline. See ctime(3) .

Also, you're trying to modify the string returned by ctime , which is a static buffer used by the C library. This can lead to a buffer overflow.

How about

fprintf(lf, "%.*s %s\n", strlen(time_string) - 1, time_string, activity);

The %.*s will will remove the trailing new-line of time_string since the specified precision is the string length - 1.

void write_log(char *activity) 
{

     FILE *lf;
     time_t current_time;
     char *time_string;
     int length = 0;
     char *line = NULL;

     current_time = time(NULL);
     time_string = ctime(&current_time);
     length = strlen(time_string) + strlen(activity) + 1;
     line = (char *)malloc(length);
     if(line){
        memset(line,'\0',length);
        strncpy(line,time_string, strlen(time_string)-1);
        strcat(line," ");
        strcat(line,activity); 
        lf = fopen("logs1.txt", "a+");
        fprintf(lf, "%s\n", line);
        fclose(lf);
        free(line);
     }
 }

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