【问题标题】:Converting an integer into a specific formatted string in C (HH:MM:SS)在 C 中将整数转换为特定格式的字符串 (HH:MM:SS)
【发布时间】:2017-09-21 11:01:06
【问题描述】:

我正在尝试创建一个基本警报,其中用户输入开始时间 (HHMMSS) 和结束时间 (HHMMSS),我希望仅以 (HH:MM:SS) 格式(24 小时时间)显示这个时间)。我目前正在这样做,但我遇到了分段错误。我对 C 中的编码相当陌生,因此非常感谢任何帮助。

int main() { 
    int present_time; 
    int time_for_alarm; 
    char outputHolder[30]; 
    printf ("Please input present time\n"); 
    scanf ("%d", &present_time); 
    printf ("Please input time for alarm\n"); 
    scanf ("%d", &time_for_alarm); 
    strftime(present_time, sizeof(present_time), "%H:%M:%S", outputHolder); 
    while (present_time < time_for_alarm) { 
        sleep(1); 
        printf ("%d\n", present_time); 
        present_time++; 
    } 
    sleep(1); 
    printf ("ALARM"); 
    return (0); 
}

【问题讨论】:

  • 请不要发布代码图片的链接。发布代码,如将其粘贴到问题中
  • 张贴代码图片是让您的问题被关闭并被否决的绝佳方式。
  • 拨打strftime 时不会收到警告吗?输出缓冲区及其大小排在第一位,最后一个参数是struct tm。此外,此功能不适用于任何像 123000 应该是时间的旧整数;它使用&lt;time.h&gt;的定义
  • 请不要在 cmets 中发布代码。这是不可读的。 Edit 你的问题。
  • 我不知道你怎么没有得到任何警告这一行:strftime(present_time, sizeof(present_time), "%H:%M:%S", outputHolder); 因为strftime 的原型如下:size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr); 所以首先你应该添加一个tm struct, struct tm *info; 然后像这样使用strftimestrftime(outputholder, 30, "%H:%M:%S", info); 请注意字节数与输出保持器的大小相同。

标签: c time


【解决方案1】:

添加必要的包括:

#include <stdio.h>
#include <time.h>
#include <unistd.h>

将错误消息减少到只是

gcc-7 -std=c11 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses -Wpedantic -Warray-bounds      46342229.c    -o 46342229
46342229.c: In function ‘main’:
46342229.c:13:14: warning: passing argument 1 of ‘strftime’ makes pointer from integer without a cast [-Wint-conversion]
     strftime(present_time, sizeof(present_time), "%H:%M:%S", outputHolder);
              ^~~~~~~~~~~~
In file included from 46342229.c:2:0:
/usr/include/time.h:205:15: note: expected ‘char * restrict’ but argument is of type ‘int’
 extern size_t strftime (char *__restrict __s, size_t __maxsize,
               ^~~~~~~~
46342229.c:13:62: warning: passing argument 4 of ‘strftime’ from incompatible pointer type [-Wincompatible-pointer-types]
     strftime(present_time, sizeof(present_time), "%H:%M:%S", outputHolder);
                                                              ^~~~~~~~~~~~
In file included from 46342229.c:2:0:
/usr/include/time.h:205:15: note: expected ‘const struct tm * restrict’ but argument is of type ‘char *’
 extern size_t strftime (char *__restrict __s, size_t __maxsize,
               ^~~~~~~~

由此可以很容易地看出,您的strftime() 调用的参数顺序错误——您可能想要strftime(outputHolder, sizeof outputHolder, "%H:%M:%S", &amp;alarm_tm); 之类的东西,其中alarm_tm 是一个适当填充的struct tm

【讨论】:

    【解决方案2】:

    我只是想补充一下strftime()的用法。

    它的原型是

    size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
    

    最后一个参数必须是struct tm的地址。

    创建一个struct tm 指针并读入它的tm_hourtm_mintm_sec 元素。

    time_t t;
    time(&t);
    struct tm *ptr=localtime(&t);
    printf ("Please input present time\n"); 
    scanf ("%2d%2d%2d", &ptr->tm_hour, &ptr->tm_min, &ptr->tm_sec); 
    

    然后使用strftime()

    strftime(outputHolder, sizeof(outputHolder), "%H:%M:%S", ptr);
    

    【讨论】:

      【解决方案3】:

      你没有以正确的方式使用 strftime。

      阅读以下两篇文章,他们解释并展示了如何在 C 中正确使用 time 和 stftime。

      1. How to print time in format: 2009‐08‐10 18:17:54.811
      2. http://man7.org/linux/man-pages/man3/strftime.3.html

      【讨论】:

        猜你喜欢
        • 2017-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-06
        • 1970-01-01
        相关资源
        最近更新 更多