免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 11552 | 回复: 7
打印 上一主题 下一主题

关于time和ctime函数(谢谢解答) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-10 16:39 |只看该作者 |倒序浏览
下列函数在打开FIFO后供写数字使用.


  1. /*
  2. * wrfifo.c - Write to a "well-known" FIFO
  3. */
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <fcntl.h>
  11. #include <limits.h>
  12. #include <time.h>

  13. int main(void)
  14. {
  15.     int fd;                                         /* Descriptor for FIFO */
  16.     int len;                                        /* Bytes written to FIFO */
  17.     char buf[PIPE_BUF];                       /* Ensure atomic writes */
  18.     time_t tp;                        /* For time call */

  19.                                                              /* Identify myself */
  20.     printf("I am %d\n", getpid());

  21.                                                         /* Open the FIFO write-only */
  22.     if((fd = open("fifo1", O_WRONLY)) < 0) {
  23.        perror("open");
  24.        exit(EXIT_FAILURE);
  25.     }

  26.                                                                  /* Generate some data to write */
  27.     while(1) {
  28.                                                                /* Get the current time */
  29.       [color=Red]  time(&tp);[/color]
  30.                                                                /* Create the string to write */
  31.         [color=Red]len = sprintf(buf, "wrfifo %d sends %s", getpid(), ctime(&tp));[/color]
  32.                                                                  /*
  33.                                                                   * Use (len + 1) because sprintf does not count
  34.                                                                   * the terminating null
  35.                                                                   */
  36.         if((write(fd, buf, len + 1)) < 0) {
  37.         perror("write");
  38.         close(fd);
  39.         exit(EXIT_FAILURE);
  40.    sleep(3);
  41.     }

  42.     close(fd);
  43.     exit(EXIT_SUCCESS);
  44. }
  45.       }
复制代码


函数time(&tp);在程序中起的作用是什么啊  !(管道已经建好了!!!在别的程序中)
请强人解答下  呵呵 谢谢了

[ 本帖最后由 郝国辉 于 2008-12-10 16:42 编辑 ]

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [报告]
发表于 2008-12-10 17:13 |只看该作者

回复 #1 郝国辉 的帖子

仅仅是记录下当前时间,可看作是调试吧,没啥特殊目的

论坛徽章:
0
3 [报告]
发表于 2008-12-10 17:13 |只看该作者

回复 #1 郝国辉 的帖子

程序中注释都写了:/* Get the current time */
楼主难道学俄语的?

论坛徽章:
0
4 [报告]
发表于 2008-12-10 17:18 |只看该作者
原帖由 meishu 于 2008-12-10 17:13 发表
程序中注释都写了:/* Get the current time */
楼主难道学俄语的?

  

我还以为他和ctime函数有什么关系  那几个单词我懂 呵呵 ctime函数是怎么回事啊??

[ 本帖最后由 郝国辉 于 2008-12-10 17:22 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-12-10 17:24 |只看该作者
原帖由 timespace 于 2008-12-10 17:13 发表
仅仅是记录下当前时间,可看作是调试吧,没啥特殊目的



ctime函数不也返回时间字符串吗??我屏幕上看到的输出时间是ctime返回的.它和time函数有关系吗?就打印输出的字符串和time有关系???

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
6 [报告]
发表于 2008-12-10 17:55 |只看该作者
原帖由 郝国辉 于 2008-12-10 17:24 发表



ctime函数不也返回时间字符串吗??我屏幕上看到的输出时间是ctime返回的.它和time函数有关系吗?就打印输出的字符串和time有关系???

time取得时间但是返回的是time_t类型,ctime把这个时间(time取得的)转换成像00:00:00 UTC, January 1, 1970这种格式,比较方便人看。

time_t time(time_t *t);
time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.

The  ctime(),  gmtime()  and  localtime()  functions all take an argument of data type time_t which represents calendar time.  When interpreted as an absolute time value, it represents the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).

论坛徽章:
0
7 [报告]
发表于 2008-12-10 19:41 |只看该作者
原帖由 ynchnluiti 于 2008-12-10 17:55 发表

time取得时间但是返回的是time_t类型,ctime把这个时间(time取得的)转换成像00:00:00 UTC, January 1, 1970这种格式,比较方便人看。


谢谢 呵呵 懂了  在去全面的学习下time函数

论坛徽章:
0
8 [报告]
发表于 2011-10-24 21:54 |只看该作者
time(&Time);是给Time 赋初始值吧,也可以用Time=time(NULL);替代,不福初始化值好像编译会错,,,
而ctime(&Time);是把time_t类型格式转化为人们方便看的格式吧~~

说得不对的地方多多指教~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP