C语言遍历目录文件并排序
有时候需要遍历目录文件,但是默认的函数并不支持指定排序的功能,现在介绍一种排序的遍历目录方式,默认按照文件名的数字进行排序
window版本
string split_pathexe(string szFullPath)//获取文件名
{
char szPath[MAX_PATH] = {0};
char szDrive[MAX_PATH] = {0};
char szDir[MAX_PATH] = {0};
char szFileName[MAX_PATH] = {0};
char szExt[MAX_PATH] = {0};
_tsplitpath(szFullPath.c_str(), szDrive, szDir, szFileName, szExt);
return string(szFileName);
}
bool CompareRules(string x, string y) // 排序回调函数,按照数字排序
{
int nLeft, nRight;
nLeft = atoi(split_pathexe(x).c_str());
nRight = atoi(split_pathexe(y).c_str());
return nLeft<nRight;
}
string GF_GetDirFile(char *szpath)//返回文件路径JSON格式,使用的jsoncpp,使用的时候自行稍作改动即可
{
list<string> mylist;
list<string>::iterator iter;
Json::Value root;
Json::FastWriter writer;
char szDir[255];
strcpy(szDir,szpath);
strcat(szDir,"\\*");
_finddata_t dir_info;
_finddata_t file_info;
intptr_t f_handle;
char tmp_path[_MAX_PATH];
if((f_handle = _findfirst(szDir, &dir_info)) != -1)
{
while(_findnext(f_handle, &file_info) == 0)
{
if(is_special_dir(file_info.name))
continue;
if(is_dir(file_info.attrib))//如果是目录,生成完整的路径
{
}
else
{
strcpy(tmp_path, szDir);
tmp_path[strlen(tmp_path) - 1] = '\0';
strcat(tmp_path, file_info.name);//生成完整的文件路径
string strPath;
strPath = tmp_path;
mylist.push_back(strPath);
//root.append(tmp_path);
}
}
_findclose(f_handle);//关闭打开的文件句柄,并释放关联资源,否则无法删除空目录
}
else
{
show_error();//若路径不存在,显示错误信息
}
mylist.sort(CompareRules);
for (iter = mylist.begin(); iter != mylist.end();++iter)
{
root.append((*iter));
}
string data = writer.write(root);
return data;
}
linux版本,linux有现成的排序遍历函数 scandir,比windows要简单
string GF_GetDirFile(char *szpath)
{
Json::Value root;
Json::FastWriter writer;
char childpath[4096];
struct dirent **namelist;
int n;
n = scandir(szpath,&namelist,0,comp);
if(n<0)
return "";
int Index = 0;
while(Index<n)
{
if(strcmp(namelist[Index]->d_name,".")==0||strcmp(namelist[Index]->d_name,"..") ==0)
{
free(namelist[Index]);
Index++;
continue;
}
string str;
sprintf(childpath,"%s%s",szpath,namelist[Index]->d_name);
if(is_dir(childpath))
{
}
else
{
root.append(childpath);
}
free(namelist[Index]);
Index++;
}
free(namelist);
string data = writer.write(root);
return data;
}
C语言遍历目录文件并排序相关推荐
- C语言字符数组显示钻石,c++两种字符串赋值方式 并介绍 C语言下遍历目录文件的方式...
c++字符串声明:一种是声明字符数组并赋值,另一种是直接声明string类 #define _CRT_SECURE_NO_WARNINGS #include #include #include &qu ...
- python遍历文件_python遍历目录文件脚本的示例
例子 自己写的一个Python遍历文件脚本,对查到的文件进行特定的处理.没啥技术含量,但是也记录一下吧. 代码如下 复制代码 #!/usr/bin/python # -*- coding: utf-8 ...
- ZH奶酪:PHP遍历目录/文件的3种方法
其实PHP中内建函数scandir()就可以返回目录下全部文件和目录了... ========================== 1.使用$obj = dir($dir)返回目录对象$obj,然后使 ...
- python-递归删除空文件夹、遍历目录文件
import os import tkinter as tk from tkinter.filedialog import (askopenfilename, # 请求打开一个文件名,用来选择需要打开 ...
- WPS JS宏之遍历目录文件
为了获取特定目录下的文件,需要借助dir()实现 var a = ThisWorkbook.Path;//取得当前文档路径 var myfile = Dir(a+"\\") ...
- PHP远程文件管理,可以给表格排序,遍历目录,时间排序
点击表格第一行,头部就可以排序了,这个列子是当前目录的 <?php $rootdir="./"; $spacenum=0; $filenum=0; $allfilesize= ...
- python 目录遍历 目录文件列表 介绍
目录 一.使用os.walk遍历所有的目录和文件 二.利用os.listdir递归获取所有的目录路径和文件路径 目录结构如下图: test---a------d------g--------g.txt ...
- Linux下 C++遍历目录文件
可参考如下代码或者参考链接博客. 测试代码: #include <iostream> #include <stdio.h> #include <unistd.h> ...
- linux易语言安装目录文件,易语言编译linux文件与相关支持库
引言: 玩了很多年易语言才发现易语言也可以编译linux下的文件,今天就试了试,在网上找了一下,还真的可以,于是收集与整理一下,学好易语言还是挺有用的,感觉. 首先现在易语言创建一个linux 程序. ...
最新文章
- 从起源、变体到评价指标,一文解读NLP的注意力机制
- iOS截取NSString字符串
- java.lang.IllegalArgumentException和org.apache.catalina.LifecycleException
- curl_init()和curl_multi_init()多线程的速度比较
- 漫谈MySQL权限安全,威力加强版
- 算法笔记_231:网格中移动字母(Java)
- Android调用系统照相机
- scikit-learn学习资源
- Android之进程间通信AIDL
- JQuery原生js ——实现剪刀石头布小游戏
- 工信部ICP备案管理系统滑动验证码破解
- 【Python笔记】Pandas时区处理
- android appwidget桌面插件,Android:AppWidget、桌面小部件
- 麒麟操作系统安装/卸载微信
- 匿名游走 Anonymous Walk
- Python绘制美国队长盾牌
- 论文阅读:Attention-based Dropout Layer for Weakly Supervised Object Localization
- jacob操作word教程
- wangEditor实现用户自定义图片大小(改源码)
- 在Windows服务器上搭建WEB环境MySQL 5.7+JDK 1.8+Tomcat 8.5并使用war包进行部署+简单运行jar程序+Tomcat Manager快速部署
热门文章
- php touppercase strtoupper,php大小写转换函数(strtolower、strtoupper)用法介绍
- 删除文本中重复的单词
- 第六周作业--需求建模
- 现在的我,不想做管理
- 可用的交换空间为 0 MB
- 技术控 | 自然语言技术在文智趋势分析产品上的应用
- 买天文望远镜必看——已知望远镜焦距、相机画幅,求视场(附代码,实时更新)
- “人生苦短,我用Python“——身份认证攻击
- 掌握IDEA的这两款插件,写“破解补丁”分分钟
- 第14/15周作业---郭志康