读取文件夹下最近三天的文件名 文件按时间产生

本文介绍了一个使用C语言编写的程序,该程序能够获取当前日期及其前两天的日期,并将这些日期格式化为特定的字符串形式(如20230101*.dbf)。此外,还演示了如何利用Windows API函数来枚举指定目录下符合日期格式的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <windows.h>
#include <stdio.h>
#include<time.h>

void setYearMonth(char *nowDay,char *yesDay,char *befDay){
	char date[15];//yyyymmdd*.dbf
	time_t now;//实例化time_t结构
	struct tm  *timenow;//实例化tm结构指针
	time(&now);
	timenow = localtime(&now);
	sprintf(date,"%4d%02d%02d*.dbf",timenow->tm_year+1900,timenow->tm_mon+1, timenow->tm_mday);
	strcpy(nowDay,date);
	now = now - 86400;
	timenow = localtime(&now);
	sprintf(date,"%4d%02d%02d*.dbf",timenow->tm_year+1900,timenow->tm_mon+1, timenow->tm_mday);
	strcpy(yesDay,date);
	now = now - 86400;
	timenow = localtime(&now);
	sprintf(date,"%4d%02d%02d*.dbf",timenow->tm_year+1900,timenow->tm_mon+1, timenow->tm_mday);
	strcpy(befDay,date);
}
void enum_path(char *cpath,char *cfile){ 
	WIN32_FIND_DATA wfd; 
	HANDLE hfd; 
	char cdir[MAX_PATH]; 
	char subdir[MAX_PATH]; 
	int r; 
	GetCurrentDirectory(MAX_PATH,cdir); 
	SetCurrentDirectory(cpath); 
	hfd = FindFirstFile(cfile,&wfd); 
	if(hfd!=INVALID_HANDLE_VALUE) { 
		do{ 
			printf("%s\n",wfd.cFileName); 
		}while(r=FindNextFile(hfd,&wfd),r!=0); 
	} 
	SetCurrentDirectory(cdir); 
} 
int main(){
	char today[15],yesday[15],befday[15];
	setYearMonth(today,yesday,befday);
	printf("%s\n",today);
	printf("%s\n",yesday);
	printf("%s\n",befday);

	enum_path("D:\\WINGDH\\bg\\num",today);
	enum_path("D:\\WINGDH\\bg\\num",yesday);
	enum_path("D:\\WINGDH\\bg\\num",befday);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值