linux--时间编程(5)

来源:互联网 发布:java有证书吗 编辑:程序博客网 时间:2024/06/05 21:04

概念

分类:1.标准时间 2.日历时间(从一个时间开始算起)

代码

#include <stdio.h>#include <stdlib.h>#include <time.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>int main(int argc, char **argv){    time_t mtime;    struct tm *mytm;    char *file_name = "fff";    struct stat mystat;    stat(file_name, &mystat);    mtime =mystat.st_mtime;    printf("cur time=%ld\n", mtime);    mytm = localtime(&mtime);    printf("sec:%d,min:%d,hour:%d,year:%d\n",        mytm->tm_sec, mytm->tm_min, mytm->tm_hour, mytm->tm_year+1900);    //将TM格式的时间转化为字符串    printf("==time is :%s\n", asctime(mytm));    //将日历时间转化为本地时间字符串    printf("==time is :%s", ctime(&mtime));    return 0;}

stat

stat函数讲解表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat(const char *file_name, struct stat *buf);函数说明:    通过文件名filename获取文件信息,并保存在buf所指的结构体stat中返回值:      执行成功则返回0,失败返回-1,错误代码存于errno错误代码:    ENOENT         参数file_name指定的文件不存在    ENOTDIR        路径中的目录存在但却非真正的目录    ELOOP          欲打开的文件有过多符号连接问题,上限为16符号连接    EFAULT         参数buf为无效指针,指向无法存在的内存空间    EACCESS        存取文件时被拒绝    ENOMEM         核心内存不足    ENAMETOOLONG   参数file_name的路径名称太长
0 0
原创粉丝点击