【问题标题】:fprintf and ctime without passing \n from ctimefprintf 和 ctime 没有从 ctime 传递 \n
【发布时间】:2012-02-24 11:21:57
【问题描述】:

我在文本文件中插入时间时遇到问题。我使用下面的代码,我得到|21,43,1,3,10,5| Wed Feb 01 20:42:32 2012,这是正常的,但我想要做的是将时间放在数字之前,例如Wed Feb 01 20:42:32 2012 |21,43,1,3,10,5| 但是,当我在 fprintf 之前使用带有 ctime 函数的 fprintf 时,我不能这样做它在 ctime 内识别 \n 的数字,因此它更改第 1 行,然后打印数字。它是这样的:

    Wed Feb 01 20:42:32 2012
    |21,43,1,3,10,5|

这是我不想要的东西...我怎样才能在不切换到文本中的下一行的情况下打印时间?提前致谢!

fprintf(file,"   |");
    for (i=0;i<6;i++)
    {
        buffer[i]=(lucky_number=rand()%49+1);       //range 1-49
        for (j=0;j<i;j++)                           
        {
            if (buffer[j]==lucky_number)
                i--;
        }
        itoa (buffer[i],draw_No,10);
        fprintf(file,"%s",draw_No);
        if (i!=5)
            fprintf(file,",");
    }
    fprintf(file,"|     %s",ctime(&t));

【问题讨论】:

    标签: c file text-files printf ctime


    【解决方案1】:

    您可以使用strftime()localtime() 的组合来创建时间戳的自定义格式字符串:

    char s[1000];
    
    time_t t = time(NULL);
    struct tm * p = localtime(&t);
    
    strftime(s, 1000, "%A, %B %d %Y", p);
    
    printf("%s\n", s);
    

    ctime使用的格式字符串就是"%c\n"

    【讨论】:

    • 不错的答案。我也试试看。
    • 不错! strftime 允许以非常灵活的方式打印不同格式的时间戳!
    【解决方案2】:

    您可以使用strtok()\n 替换为\0。这是一个最小的工作示例:

    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    int main() {
        char *ctime_no_newline;
        time_t tm = time(NULL);
    
        ctime_no_newline = strtok(ctime(&tm), "\n");
        printf("%s - [following text]\n", ctime_no_newline);
    
        return 0;
    }
    

    输出:

    Sat Jan  2 11:58:53 2016 - [following text]
    

    【讨论】:

      【解决方案3】:

      只需使用 %.19s :

      struct timeb timebuf;
      char *now;
      
      ftime( &timebuf );
      now = ctime( &timebuf.time );
      
      /* Note that we're cutting "now" off after 19 characters to avoid the \n
      that ctime() appends to the formatted time string.   */
      snprintf(tstring, 30, "%.19s", now);  // Mon Jul 05 15:58:42
      

      【讨论】:

      • 打印 19 个字符将省略年份。如果要包含年份,请改用"%.24s"
      【解决方案4】:
      1. ctime() 的返回值复制到一个临时字符串,从该临时字符串中删除'\n',然后打印该临时字符串。
      2. 使用 printf 转换的(字段宽度和)精度,仅打印从 ctime() 返回的前 24 个字符。

      【讨论】:

        【解决方案5】:

        在 c++11 中你可以这样做:

        #include <iostream>
        #include <chrono>
        #include <iomanip>
        using namespace std;
        using namespace chrono;
        
        // Prints UTC timestamp
        void printTime() {
            time_point<system_clock> now = system_clock::now();
            time_t now_time = system_clock::to_time_t(now);
        
            auto gmt_time = gmtime(&now_time);
            auto timestamp = std::put_time(gmt_time, "%Y-%m-%d %H:%M:%S");
            cout << timestamp << endl;
        }
        

        输出:

        2017-06-05 00:31:49

        【讨论】:

        • 我很惊讶使用chrono 的答案如此之少。谢谢 Caner,太棒了。
        • 请注意using namespace std; using namespace chrono; 的顺序很重要。反转它,它不会编译。
        【解决方案6】:

        怎么样:

        char *p;
        int len;
        
        /* ... */
        
        p = ctime(&t);
        len = strlen(p);
        fprintf(file,"|     %.*s", len - 1, p);
        

        这样它只打印字符串减去最后一个字符(即\n)。

        【讨论】:

        • 或:printf("%.*s", (int)strlen(p)-1, p);
        【解决方案7】:

        我是在获取ctime字符串后这样做的:

        #include <string.h>
        ...
        myctime[ strlen(myctime) - 1 ] = '\0';
        

        这只是用字符串终止字符覆盖 ctime 回车,有效地用两个 '\0' 字符而不是一个字符来终止字符串。 (ctime 一开始就这样做似乎很奇怪。)

        【讨论】:

          【解决方案8】:

          只需将 'length - 1' 字节复制到另一个字符串。

          strncpy( newString, draw_No, strlen(draw_no) - 1 );
          

          【讨论】:

          • 之后对字符串进行 nul 终止可能是个好主意。
          【解决方案9】:

          简单地说:

              c_time_string = ctime(&current_time);
              len_of_new_line = strlen(c_time_string) - 1;
              c_time_string[len_of_new_line] = '\0';
          

          这实际上会做的是用空终止符替换 ctime 数组的 strlen - 1 个字符(在这种情况下为新行字符) - 它从结尾 '\n' 中删除新行字符并缩短 1 个字符的数组.

          如果 strlen 之前是 25,那么之后应该是 24。

          【讨论】:

            猜你喜欢
            • 2013-01-12
            • 2012-09-11
            • 2011-08-29
            • 2012-05-18
            • 2011-08-02
            • 2016-07-05
            • 1970-01-01
            • 2011-08-27
            • 1970-01-01
            相关资源
            最近更新 更多