C++用 _findfirst 和 _findnext 查找文件
#include <io.h>
#include <iostream>
#include <fstream>
using namespace std; bool transfer(string fileName, int exeNum );
void dfsFolder(string folderPath, ofstream &fout); int main()
{
_finddata_t file;
int k;
long HANDLE;
k = HANDLE = _findfirst("*.*", &file);
while (k != -)
{
cout << file.name << endl;
k = _findnext(HANDLE, &file);
}
_findclose(HANDLE); transfer("C:\\Windows\\*.exe", );
ofstream o_fstream; dfsFolder("E:\\\WHU\\Study", o_fstream); return ;
} //_findfirst 函数返回的是匹配到文件的句柄,数据类型为long。
//遍历过程可以指定文件类型,这通过FileName的赋值来实现,例如要遍历C : \WINDOWS下的所有.exe文件 bool transfer(string fileName , int exeNum)
{
_finddata_t fileInfo;
long handle = _findfirst(fileName.c_str(), &fileInfo); if (handle == -1L)
{
cerr << "failed to transfer files" << endl;
return false;
} do
{
exeNum++;
cout << fileInfo.name << endl;
} while (_findnext(handle, &fileInfo) == );
cout << " .exe files' number: " << exeNum << endl; return true;
} //遍历文件夹及其子文件夹下所有文件。操作系统中文件夹目录是树状结构,使用深度搜索策略遍历所有文件。用到_A_SUBDIR属性 //在判断有无子目录的if分支中,由于系统在进入一个子目录时,匹配到的头两个文件(夹)是"."(当前目录),".."(上一层目录)。
//需要忽略掉这两种情况。当需要对遍历到的文件做处理时,在else分支中添加相应的代码就好 void dfsFolder(string folderPath, ofstream &fout)
{
_finddata_t FileInfo;
string strfind = folderPath + "\\*";
long Handle = _findfirst(strfind.c_str(), &FileInfo); if (Handle == -1L)
{
cerr << "can not match the folder path" << endl;
exit(-);
}
do{
//判断是否有子目录
if (FileInfo.attrib & _A_SUBDIR)
{
//这个语句很重要
if ((strcmp(FileInfo.name, ".") != ) && (strcmp(FileInfo.name, "..") != ))
{
string newPath = folderPath + "\\" + FileInfo.name;
dfsFolder(newPath, fout);
}
}
else
{
fout<<folderPath.c_str() << "\\" << FileInfo.name << " ";
cout << folderPath.c_str() << "\\" << FileInfo.name << endl;
}
} while (_findnext(Handle, &FileInfo) == ); _findclose(Handle);
fout.close();
} //#include <iostream>
//#include <string>
//#include <io.h>
//using namespace std;
//
//int main()
//{
// _finddata_t file;
// long longf;
// string tempName;
// //_findfirst返回的是long型; long __cdecl _findfirst(const char *, struct _finddata_t *)
// if ((longf = _findfirst("E:\\WHU\\Study\\*.*", &file)) == -1l)
// {
// cout << "文件没有找到!\n";
// return 0;
// }
// do
// {
// cout << "文件列表:\n";
// tempName = file.name;
// if (tempName[0] == '.')
// continue;
// cout << file.name<<endl;
//
// if (file.attrib == _A_NORMAL)
// {
// cout << " 普通文件 ";
// }
// else if (file.attrib == _A_RDONLY)
// {
// cout << " 只读文件 ";
// }
// else if (file.attrib == _A_HIDDEN)
// {
// cout << " 隐藏文件 ";
// }
// else if (file.attrib == _A_SYSTEM)
// {
// cout << " 系统文件 ";
// }
// else if (file.attrib == _A_SUBDIR)
// {
// cout << " 子目录 ";
// }
// else
// {
// cout << " 存档文件 ";
// }
// cout << endl;
// } while (_findnext(longf, &file) == 0);//int __cdecl _findnext(long, struct _finddata_t *);如果找到下个文件的名字成功的话就返回0,否则返回-1
//
// _findclose(longf);
//
// return 0;
//}
C++用 _findfirst 和 _findnext 查找文件的更多相关文章
- locate 最快的查找文件的命令 NB
我见过最NB的查找文件最快的命令 [root@NB data]# locate teamviewer. /data/Software/teamviewer.i686.rpm /home/ok/.loc ...
- Linux如何搜索查找文件里面内容
在Linux系统当中,如何搜.索查找文件里面的内容呢? 这个应该是系统维护.管理当中遇到最常见的需求.那么下面介绍,总结一下如何搜索.查找文件当中的内容. 搜索.查找文件当中的内容,一般最常用的是gr ...
- Linux下查找文件:which、whereis、locate、find 命令的区别
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.which 查看可执行文件的位置,通过环境变量查whereis 查看文件的位置,通过数据库查,每 ...
- linux 根据文件大小查找文件
inux下的find命令用来查找文件,通过man find就知道它是无所不能的.所以按照文件大小来查找文件就不在话下.从man find搜索size,可以看到如下信息: -size n[cwbkMG] ...
- Linux 利用 locate 和 find 查找文件
Linux 利用 locate 和 find 查找文件 命令 locate 用于快速查找文件.文件夹.此命令并没有在磁盘上查找所有文件,而是在预先建立的数据库里进行搜索.可以使用 updatedb 命 ...
- Linux 查找文件
find 查找目录 -name "文件名"find / -name "php.ini"locate 文件名locate php.ini 一:locate命令 l ...
- 53-whereis 查找文件
查找文件 whereis [options] file 参数 file 是whereis需要查找的文件,这些文件属于原始代码,二进制文件或是帮助文件 选项 -b 只查找二进 ...
- 14-find 查找文件
find - search for files in a directory hierarchy 查找文件 [语法]: find [选项] [参数] [功能介绍] find命令用来在指定目录下查找文件 ...
- Linux里如何查找文件内容
Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件g ...
随机推荐
- php发送邮件(TP5)
先百度搜索phpmailer 下载phpmailer函数包 放到/vendor/下,这是tp5扩展类库目录 然后你需要一个已经开启了SMTP服务的邮箱,作为发送者邮箱,QQ邮箱163邮箱是需要自己开 ...
- TrueCrypt 7.1a Hashes
Here are the SHA256, SHA1, and MD5 hashes of all TrueCrypt version 7.1a files. The signature of the ...
- 增加临时表空间组Oracle11g单实例
#需求,测试库与生产库,临时表空间同步一致 #经过查询生产环境,数据库默认临时表空间,为临时表空间组,有三个成员,三个临时表空间,每个临时表空间一个数据文件,自动扩展 #使用临时表空间组的优点,减少不 ...
- LINUX7安装Oracle11g单实例小结
LINUX7安装Oracle11g遇到问题如下,记录 添加组: groupadd -g 1000 oinstall #报错:提示组被占用 #useradd: group 'oinstall' does ...
- 【opencv基础】OpenCV installation stuck at [ 98%] Built target opencv_perf_stitching with no error
前言 按照官网步骤安装opencv的过程中进行到98%时一直没有继续进行. 原因 后台一直在编译运行,只需等待即可,参考here: well, turns out it gets stuck for ...
- [LeetCode&Python] Problem 824. Goat Latin
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- IE8的兼容问题
1: rgba失效的问题: 在添加rgba的类名内加上:filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#7f00000 ...
- HDU 1875:畅通工程再续(最小生成树)
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- Thread sleep() wait()
学艺不精,先总结一下两者的区别: 对比项 wait sleep 类所属 Object Thread,static方法 使用 在synchronised block中(包括notify,notifyAl ...
- Ubuntu防火墙简单设置
http://wiki.ubuntu.org.cn/UFW防火墙简单设置 http://wiki.ubuntu.org.cn/Ufw使用指南 Ubuntu默认安装内置ufw防火墙,简单使用如下: su ...