700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > c 语言 去除字符串换行符 【C编程】去掉显示时间的换行符!

c 语言 去除字符串换行符 【C编程】去掉显示时间的换行符!

时间:2021-01-10 16:41:30

相关推荐

c 语言 去除字符串换行符 【C编程】去掉显示时间的换行符!

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

为了去掉输出日期中的换行符,如下编程:

/*=========================================

* Copyright (c) , 逐风墨客

* All rights reserved.

*

* 文件名称:study_nontime.c

* 运行环境:Linux操作系统

* 功能描述:去掉显示时间的换行符!

=========================================*/

#include

#include

#include

#include // 调用sleep()函数

#include

const char *show_realtime(time_t t);

int main(void)

{

time_t t = 0;

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 t)

* 输入参数:t-时间种子

* 输出参数:无

* 返回值:buf-不带换行符的字符串时间

*******************************************/

const char *show_realtime(time_t t)

{

static char buf[32];

char *p = NULL;

time(&t);

strcpy(buf, ctime(&t));

p = strchr(buf, '\n');

*p = '\0';

return buf;

}

程序运行结果:

程序运行结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。