C++获取一个文件夹下的所有文件名(转)

简介:

C++获取一个文件夹下的所有文件名
window + vs2005:

#ifndef FUNC_H
#define FUNC_H

#include <string>
#include <vector>
#include <fstream>
#include <windows.h>

using namespace std;

vector<string> & get_filelist(char *foldname)
{

vector<string> flist;
HANDLE file;
WIN32_FIND_DATA fileData;
char line[1024];
wchar_t fn[1000];
mbstowcs(fn,(const char*)foldname,999);
file = FindFirstFile(fn, &fileData);
FindNextFile(file, &fileData);
while(FindNextFile(file, &fileData)){
wcstombs(line,(const wchar_t*)fileData.cFileName,259);
flist.push_back(line);
}
return flist;
}
#endif



linux:

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <errno.h>
int main(int argc,char *argv[])
{

DIR *dp;
struct dirent *dirp;
int n=0;
if (argc!=2)
{
printf("a single argument is required\n");
return 0;
}
if((dp=opendir(argv[1]))==NULL)
printf("can't open %s",argv[1]);
while (((dirp=readdir(dp))!=NULL) && (n<=50))
{
n++;
printf("%s\n",dirp->d_name);
}
printf("\n");
closedir(dp);

return 0;
}

某牛人封装好的类

// bb.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include "stdlib.h"
#include "direct.h"
#include "string.h"
#include "io.h"
#include "stdio.h"
#include <iostream>
using namespace std;
class CBrowseDir
{
protected:
//存放初始目录的绝对路径,以'\'结尾
char m_szInitDir[_MAX_PATH];

public:
//缺省构造器
CBrowseDir();

//设置初始目录为dir,如果返回false,表示目录不可用
bool SetInitDir(const char *dir);

//开始遍历初始目录及其子目录下由filespec指定类型的文件
//filespec可以使用通配符 * ?,不能包含路径。
//如果返回false,表示遍历过程被用户中止
bool BeginBrowse(const char *filespec);

protected:
//遍历目录dir下由filespec指定的文件
//对于子目录,采用迭代的方法
//如果返回false,表示中止遍历文件
bool BrowseDir(const char *dir,const char *filespec);

//函数BrowseDir每找到一个文件,就调用ProcessFile
//并把文件名作为参数传递过去
//如果返回false,表示中止遍历文件
//用户可以覆写该函数,加入自己的处理代码
virtual bool ProcessFile(const char *filename);

//函数BrowseDir每进入一个目录,就调用ProcessDir
//并把正在处理的目录名及上一级目录名作为参数传递过去
//如果正在处理的是初始目录,则parentdir=NULL
//用户可以覆写该函数,加入自己的处理代码
//比如用户可以在这里统计子目录的个数
virtual void ProcessDir(const char *currentdir,const char *parentdir);
};

CBrowseDir::CBrowseDir()
{
//用当前目录初始化m_szInitDir
getcwd(m_szInitDir,_MAX_PATH);
//如果目录的最后一个字母不是'\',则在最后加上一个'\'
int len=strlen(m_szInitDir);
if (m_szInitDir[len-1] != '\\')
strcat(m_szInitDir,"\\");
}

bool CBrowseDir::SetInitDir(const char *dir)
{
//先把dir转换为绝对路径
if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)
return false;

//判断目录是否存在
if (_chdir(m_szInitDir) != 0)
return false;

//如果目录的最后一个字母不是'\',则在最后加上一个'\'
int len=strlen(m_szInitDir);
if (m_szInitDir[len-1] != '\\')
strcat(m_szInitDir,"\\");

return true;
}

bool CBrowseDir::BeginBrowse(const char *filespec)
{
ProcessDir(m_szInitDir,NULL);
return BrowseDir(m_szInitDir,filespec);
}

bool CBrowseDir::BrowseDir(const char *dir,const char *filespec)
{
_chdir(dir);

//首先查找dir中符合要求的文件
long hFile;
_finddata_t fileinfo;
if ((hFile=_findfirst(filespec,&fileinfo)) != -1)
{
do
{
//检查是不是目录
//如果不是,则进行处理
if (!(fileinfo.attrib & _A_SUBDIR))
{
char filename[_MAX_PATH];
strcpy(filename,dir);
strcat(filename,fileinfo.name);
cout << filename << endl;
if (!ProcessFile(filename))
return false;
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
//查找dir中的子目录
//因为在处理dir中的文件时,派生类的ProcessFile有可能改变了
//当前目录,因此还要重新设置当前目录为dir。
//执行过_findfirst后,可能系统记录下了相关信息,因此改变目录
//对_findnext没有影响。
_chdir(dir);
if ((hFile=_findfirst("*.*",&fileinfo)) != -1)
{
do
{
//检查是不是目录
//如果是,再检查是不是 . 或 ..
//如果不是,进行迭代
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name,".") != 0 && strcmp
(fileinfo.name,"..") != 0)
{
char subdir[_MAX_PATH];
strcpy(subdir,dir);
strcat(subdir,fileinfo.name);
strcat(subdir,"\\");
ProcessDir(subdir,dir);
if (!BrowseDir(subdir,filespec))
return false;
}
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
return true;
}

bool CBrowseDir::ProcessFile(const char *filename)
{
return true;
}

void CBrowseDir::ProcessDir(const char
*currentdir,const char *parentdir)
{
}

//从CBrowseDir派生出的子类,用来统计目录中的文件及子目录个数
class CStatDir:public CBrowseDir
{
protected:
int m_nFileCount; //保存文件个数
int m_nSubdirCount; //保存子目录个数

public:
//缺省构造器
CStatDir()
{
//初始化数据成员m_nFileCount和m_nSubdirCount
m_nFileCount=m_nSubdirCount=0;
}

//返回文件个数
int GetFileCount()
{
return m_nFileCount;
}

//返回子目录个数
int GetSubdirCount()
{
//因为进入初始目录时,也会调用函数ProcessDir,
//所以减1后才是真正的子目录个数。
return m_nSubdirCount-1;
}

protected:
//覆写虚函数ProcessFile,每调用一次,文件个数加1
virtual bool ProcessFile(const char *filename)
{
m_nFileCount++;
return CBrowseDir::ProcessFile(filename);
}

//覆写虚函数ProcessDir,每调用一次,子目录个数加1
virtual void ProcessDir
(const char *currentdir,const char *parentdir)
{
m_nSubdirCount++;
CBrowseDir::ProcessDir(currentdir,parentdir);
}
};

void main()
{
//获取目录名
char buf[256];
printf("请输入要统计的目录名:");
gets(buf);

freopen("output.txt","w",stdout);

//构造类对象
CStatDir statdir;

//设置要遍历的目录
if (!statdir.SetInitDir(buf))
{
puts("目录不存在。");
return;
}

//开始遍历
statdir.BeginBrowse("*.*");
// printf("文件总数: %d\n子目录总数:%d\n",statdir.GetFileCount(),statdir.GetSubdirCount());

本文转自博客园知识天地的博客,原文链接:C++获取一个文件夹下的所有文件名(转),如需转载请自行联系原博主。
}

相关文章
|
6月前
|
存储 C++ Python
C++-筛选文件夹中符合要求的文件并拷贝出来(以手机号码查找为例)
C++-筛选文件夹中符合要求的文件并拷贝出来(以手机号码查找为例)
|
12月前
|
Java API Android开发
Android C++系列:访问Assets 文件夹.md
assets目录是Android的一种特殊目录,用于放置APP所需的固定文件,且该文件被打包到APK中时,不会被编码到二进制文件。 Android还存在一种放置在res下的raw目录,该目录与assets目录不同。
271 0
|
存储 文件存储 C++
C++ 实现输出某个文件夹下所有文件名称,finddata_t、findfirst、findnext函数祥讲细讲解
利用C++实现输出某个文件夹下的文件名,需要用到的函数及其数据类型;
|
算法 C++
使用VC++压缩解压缩文件夹
前言  项目中要用到一个压缩解压缩的模块, 看了很多文章和源代码, 都不是很称心, 现在把我自己实现的代码和大家分享. 要求:          1.使用Unicode(支持中文).        2.
1314 0
|
C++ 编译器
c++/c 获取cpp文件行号跟文件名
编译器内置宏: 先介绍几个编译器内置的宏定义,这些宏定义不仅可以帮助我们完成跨平台的源码编写,灵活使用也可以巧妙地帮我们输出非常有用的调试信息。 ANSI C标准中有几个标准预定义宏(也是常用的): __LINE__:在源代码中插入当前源代码行号; __FILE__:在源文件中插入当前源文件名; __DATE__:在源文件中插入当前的编译日期 __TIME__:在源文件中插入当前编译时间; __STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1; __cplusplus:当编写C++程序时该标识符被定义。
850 0
|
存储 C++
C++ 存储指定文件夹下的文件
  指定文件夹地址,然后读取文件夹内容。   file.h #include &lt;string&gt; #include &lt;vector&gt; #include &lt;io.h&gt; /* * 定义于io.h * 1、long _findfirst64i32(const char * _Filename,struct _finddata64i32_t * _FindDa
1188 0
|
1天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
16 0
|
1天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
12 0
|
4天前
|
存储 安全 C语言
【C++】string类
【C++】string类

热门文章

最新文章