C语言 文件中时间与当前系统时间 时间差比较(比较结果返回为天数)最好是调用子函数解决

C语言 文件中时间与当前系统时间 时间差比较(比较结果返回为天数)最好是调用子函数解决,第1张

#include <stdioh>

#include <stdlibh>

#include <timeh>

int days(char begin_time)   //格式: "20180914000000"  //2018-9-14 0点0分0秒 

{

struct tm tm1;

time_t time1,time_now;

sscanf(begin_time,"%4d%2d%2d%2d%2d%2d",&tm1tm_year,&tm1tm_mon,&tm1tm_mday,&tm1tm_hour,&tm1tm_min,&tm1tm_sec);

tm1tm_year-=1900;

tm1tm_mon--;

tm1tm_isdst=-1;

time1=mktime(&tm1);  //构造time1 

time_now=time(NULL);

tm1=(localtime(&time_now));      //当时日期

return (time_now-time1)/(360024); //返回相差天数 

}

int main()

{

char time_in_file[30]={"20180914000000\0"};

printf("\n%d",days(time_in_file));

return 0;

}

//获取指定目录下的所有文件列表 author:wangchangshaui jlu

char getFileNameArray(const char path, int fileCount)

{

int count = 0;

char fileNameList = NULL;

struct dirent ent = NULL;

DIR pDir;

char dir[512];

struct stat statbuf;

//打开目录

if ((pDir = opendir(path)) == NULL)

{

myLog("Cannot open directory:%s\n", path);

return NULL;

}

//读取目录

while ((ent = readdir(pDir)) != NULL)

{ //统计当前文件夹下有多少文件(不包括文件夹)

//得到读取文件的绝对路径名

snprintf(dir, 512, "%s/%s", path, ent->d_name);

//得到文件信息

lstat(dir, &statbuf);

//判断是目录还是文件

if (!S_ISDIR(statbufst_mode))

{

count++;

}

} //while

//关闭目录

closedir(pDir);

// myLog("共%d个文件\n", count);

//开辟字符指针数组,用于下一步的开辟容纳文件名字符串的空间

if ((fileNameList = (char) myMalloc(sizeof(char) count)) == NULL)

{

myLog("Malloc heap failed!\n");

return NULL;

}

//打开目录

if ((pDir = opendir(path)) == NULL)

{

myLog("Cannot open directory:%s\n", path);

return NULL;

}

//读取目录

int i;

for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)

{

if (strlen(ent->d_name) <= 0)

{

continue;

}

//得到读取文件的绝对路径名

snprintf(dir, 512, "%s/%s", path, ent->d_name);

//得到文件信息

lstat(dir, &statbuf);

//判断是目录还是文件

if (!S_ISDIR(statbufst_mode))

{

if ((fileNameList[i] = (char) myMalloc(strlen(ent->d_name) + 1))

== NULL)

{

myLog("Malloc heap failed!\n");

return NULL;

}

memset(fileNameList[i], 0, strlen(ent->d_name) + 1);

strcpy(fileNameList[i], ent->d_name);

myLog("第%d个文件:%s\n", i, ent->d_name);

i++;

}

} //for

//关闭目录

closedir(pDir);

fileCount = count;

return fileNameList;

}

timeh头文件提供对时间 *** 作的一些函数,clock()是程序开始到调用的毫秒数。

time_tt_begin,t_end;

t_begin=clock();//记录开始时间

dosomething();//调用函数

t_end=clock();//记录结束时间

printf("Timeused=%21f\n",(double)(t_end-t_begin)/CLOCKS_PER_SEC);//显示函数调用时间

扩展资料

c语言中timeh头文件的使用

#include<stdioh>

#include<stdlibh>

#include<timeh>

intmain(void)

{

longi=10000000L;

clock_tstart,finish;

doubleduration;//测量一个事件持续的时间

printf("Timetodo%ldemptyloopsis",i);

start=clock();

while(i--)

finish=clock();

duration=(double)(finish-start)/CLOCKS_PER_SEC;//clock()是以毫秒为单位计算时间的所以除以CLOCKS_PER_SEC这是timeh里面定义的一个常量

printf("%fseconds\n",duration);

system("pause");

}

以上就是关于C语言 文件中时间与当前系统时间 时间差比较(比较结果返回为天数)最好是调用子函数解决全部的内容,包括:C语言 文件中时间与当前系统时间 时间差比较(比较结果返回为天数)最好是调用子函数解决、C语言:如何得到指定地址的文件夹中所有文件的文件名和其修改时间 包括子文件内的、C语言中time.h头文件中对时间的 *** 作具体是怎样的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10060401.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存