使用readdir读取目录文件并按时间排序输出

使用readdir读取目录文件并按时间排序输出

最近需要使用readdir遍历目录下文件,但是发现取出文件的顺序都是随机的,百度发现可以使用scandir函数按照规则排序:

https://blog.csdn.net/krens/article/details/78365382?utm_source=blogxgwz3

刚学C语言,所以决定自己动手写一下,写这个也是给自己做笔记,其中使用了readdir和stat函数,关于这两个函数的讲解:

https://www.cnblogs.com/jikexianfeng/p/5742887.html
https://www.cnblogs.com/gangzilife/p/9083470.html

下面贴一下实现代码:

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h> 
#include<unistd.h> 
#include<stdlib.h>
#include<string.h>

#define   MAX_NAME_LEN   256+1

typedef struct  file_list
{
   
	time_t   file_ctime;
	char*     file_name;
	struct file_list* next;
}file_list_t;
	

void main(int argc, char** argv)
{
   
	DIR*   			 dir;
	struct dirent*   ptr;
	int 			 i;
	int              flag = 0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值