C算法&面试题C语言

【C编程】去掉显示时间的换行符!

2020-05-15  本文已影响0人  逐风墨客

  C语言中char *ctime(const time_t *time);函数将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,输出4个字节日期字符串格式为"Wed Jun 30 21 :49 :08 2020\n"。
  为了去掉输出日期中的换行符,如下编程:

/*=========================================
* Copyright (c) 2020, 逐风墨客
* All rights reserved.
* 功能描述:去掉显示时间的换行符!
* 所用函数:const char *show_realtime(time_t time); 
* 返回值:不带换行符的时间值
=========================================*/

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

int main(void)
{
  const char *show_realtime(time_t time); 
  time_t t = 0;
  
  system("clear"); // linux系统清屏
  
  printf("The current time is : %s\n", show_realtime(t));
  sleep(5);
  printf("The current time after 5 seconds is : %s\n", show_realtime(t));
  return 0;  
}

const char *show_realtime(time_t time)
{
  static char buf[32];
  char *p = NULL;

  time(&time);
  strcpy(buf, ctime(&time));
  p = strchr(buf, '\n');
  *p = '\0';
  return buf;
}
上一篇 下一篇

猜你喜欢

热点阅读