简体   繁体   English

如何在C中的文件中将当前日期和时间以及字符串打印到一行?

[英]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. 我的C程序中有一个日志文件,我试图在每次用户执行查询时向其中输入条目。 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. 我认为这与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 : man ctime

The call ctime(t) is equivalent to asctime(localtime(t)) . 调用ctime(t)等效于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" 它将日历时间t转换为以“ Wed Jun 30 21:49:08 1993 \\ n”形式的空终止字符串。

so a \\n character is included in the resulting string. 因此\\n字符包含在结果字符串中。 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. 出于上述原因,这一点很重要, 并且未知该缓冲区的大小,因此由于可能的缓冲区溢出,将其用作strcat()的目标是不安全的。 Instead of performing a strcat() remove the new line character and perform two writes to the file; 代替执行strcat()删除换行符并执行两次写入文件; one for the time_string and one for the activity\\n . 一个用于time_string ,另一个用于activity\\n

The string returned by ctime ends with a newline. ctime返回的字符串以换行符结尾。 See ctime(3) . 参见ctime(3)

Also, you're trying to modify the string returned by ctime , which is a static buffer used by the C library. 另外,您正在尝试修改ctime返回的字符串, ctime是C库使用的静态缓冲区。 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. %.*s将删除time_string的尾随换行符,因为指定的精度是字符串长度-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);
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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