您的位置:首页 > 编程语言 > C语言/C++

遍历一个目录下的所有文件

2014-04-03 16:25 344 查看
    今天整理数据库的时候发现,需要统计下不同分辨率的图片的数目的多少,本来想着按照图片的大小排个序,然后就可以人工统计,后面发现大小相同的图片,分辨可不一样相同,那就没办法人工统计了。没办法,只好用电脑来完成这种不用脑的体力活了。
    这个程序主要利用的QT的QDir类来实现的(QT真是好强大)。
    下面来看代码。
这个是获取一个文件夹下面的文件名字的函数

#include <QDir>
#include <iostream>
/**
* @brief GetFilesInDirectory This function get the
* @param Directory
* @return A list of the names of all the files, or An empty list if the
*          directory is unreadable, does not exist,or dose not contain
*          any files.
*
* @author  sheng
* @version 1.0.0
* @date    2014-04-03
*
* @history   <author>      <date>         <description>
*             sheng      2014-04-03         build the function
*
*/
QStringList GetFilesInDirectory(const QString &Directory)
{
// set the directory
QDir Dir(Directory);

// set the filter and sorting rules.
Dir.setFilter(QDir::Files | QDir::NoSymLinks);
Dir.setSorting(QDir::Size | QDir::Name);

// return the FileList in the directory
return Dir.entryList();
}


下面是测试程序

#include <QDir>
#include <Functions.h>
#include <iostream>

void Test_GetFilesInDirectory()
{

QStringList FileList =
GetFilesInDirectory("E:/");

std::cout << "The size of the FileList is " << FileList.size()
<< std::endl;

for (int i = 0; i < FileList.size(); i++)
{
QString FileName = FileList.at(i);
std::cout << "No." << (i + 1) << "Picture's name is ";
std::cout << FileName.toLocal8Bit().constData() << std::endl;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息