列出C盘下所有文件的函数

heaven33 2009-07-02 10:16:49
请问用c++写一个列出c盘下所有文件的函数,怎么写?
...全文
106 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangyunzhao 2009-07-03
  • 打赏
  • 举报
回复
写一个跨平台的吧,查询指定路径下所有文件包括路径。
rendao0563 2009-07-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lingyin55 的回复:]
C/C++ code
#include <windows.h>
void main()
{
const char *command = "dir /s c:\\";
system(command);
system("pause");
}
[/Quote]

T
yuyunliuhen 2009-07-03
  • 打赏
  • 举报
回复
vector<USER_DEFINE_FIND_DATA> CFileManage::Win32RecurseDirectory(TCHAR* strDirectoryName)
{
TCHAR *strPathName = new TCHAR[BUFSIZE];
TCHAR *strTempPath = new TCHAR[BUFSIZE];
BOOL bSearchFile = FALSE;
BOOL bFinished = FALSE;
DWORD dwPos = 0;
USER_DEFINE_FIND_DATA Filedata;
ZeroMemory(&Filedata,sizeof(Filedata));
HANDLE hFindFile = INVALID_HANDLE_VALUE;

//save current path to a strPathName variable
_tcscpy(strPathName,strDirectoryName);

//add a \ to the end of strPathName
if (strPathName[_tcslen(strPathName)-1] != _T('\\'))
{
_tcscat(strPathName,_T("\\"));
}
//add a * to the end of strPathName
_tcscat(strPathName,_T("*.*"));

//find the first file in the directory,if succeeds,return value is a search handle used in a subsequent call to FindNextFile or FindClose.
bSearchFile = (INVALID_HANDLE_VALUE !=(hFindFile = FindFirstFile(strPathName,&Filedata)));
if (bSearchFile)
{
//if direction is current or subdirectories ,ignore it
if (_tcscmp(Filedata.cFileName,_T(".")) && _tcscmp(Filedata.cFileName,_T("..")))
{
m_LogOutput.FormatOutput(_T(" %s\n"),Filedata.cFileName);
}
while (!bFinished)
{
//Continues a file search next file,recurse calls RecurseDirectory,until no file or direction left
if (FindNextFile(hFindFile,&Filedata))
{
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && _tcscmp(Filedata.cFileName,_T(".")) && _tcscmp(Filedata.cFileName,_T("..")))
{
_tcscpy(strTempPath,strDirectoryName);
_tcscat(strTempPath,Filedata.cFileName);
_tcscat(strTempPath,_T("\\"));
m_LogOutput.FormatOutput(_T(" %s\n"),Filedata.cFileName);
_tcscpy(Filedata.strFilePath,strTempPath);
_tcscat(Filedata.strFilePath,Filedata.cFileName);
m_vFileInfo.push_back(Filedata);
Win32RecurseDirectory(strTempPath);
}
else if (_tcscmp(Filedata.cFileName,_T(".")) && _tcscmp(Filedata.cFileName,_T("..")))
{
m_LogOutput.FormatOutput(_T(" %s\n"),Filedata.cFileName);
_tcscpy(Filedata.strFilePath,strDirectoryName);
_tcscat(Filedata.strFilePath,Filedata.cFileName);
m_vFileInfo.push_back(Filedata);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES )
{
bFinished = TRUE;
}
else
{
bFinished = TRUE;
}
}
}
}
else
{
m_LogOutput.FormatOutput(_T("Invalid File Handle. GetLastError reports %d\n"), GetLastError ());
}
delete []strPathName;
delete []strTempPath;
FindClose(hFindFile);
return m_vFileInfo;
}


woods2001 2009-07-02
  • 打赏
  • 举报
回复
用命令行参数
gykgod 2009-07-02
  • 打赏
  • 举报
回复
参考MFC的CFileFind
sunnywyg 2009-07-02
  • 打赏
  • 举报
回复
2楼的只能列一级目录吧?要把所有的文件找出来,应该是只能递归,不过估计要很久...
大前置 2009-07-02
  • 打赏
  • 举报
回复
递归
lingyin55 2009-07-02
  • 打赏
  • 举报
回复

#include <windows.h>
void main()
{
const char *command = "dir c:\\";
system(command);
system("pause");
}
baihacker 2009-07-02
  • 打赏
  • 举报
回复


typedef void (*ENUM_FILES_FUNCTION)(const TCHAR*, LPWIN32_FILE_ATTRIBUTE_DATA);

#define SYS_IS_DIRECTORY(f) (f & FILE_ATTRIBUTE_DIRECTORY)
#define SYS_IS_HIDDEN(f) (f & FILE_ATTRIBUTE_HIDDEN)
#define SYS_IS_READONLY(f) (f & FILE_ATTRIBUTE_READONLY)
#define SYS_IS_SYSTEM(f) (f & FILE_ATTRIBUTE_SYSTEM)
extern void EnumFiles(const TCHAR*, ENUM_FILES_FUNCTION);

void EnumFiles(const TCHAR* szSourceDir, ENUM_FILES_FUNCTION callback_fun)
{
if (!szSourceDir) return;

WIN32_FIND_DATA FindFileData;
WIN32_FILE_ATTRIBUTE_DATA FindFileAttr;
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR currDir[MAX_PATH];
TCHAR Result[MAX_PATH];

_tcsncpy(currDir, szSourceDir, _tcslen(szSourceDir) + 1);
_tcsncat(currDir, _T("\\*"), 3);

hFind = FindFirstFile(currDir, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE) return;
do
{
if (*FindFileData.cFileName == '.') continue;
::_stprintf(Result, _T("%s\\%s"), szSourceDir, FindFileData.cFileName);
if (callback_fun && GetFileAttributesEx(Result, GetFileExInfoStandard, &FindFileAttr))
{

(*callback_fun)(Result, &FindFileAttr);
}
} while (FindNextFile(hFind, &FindFileData));
}
kiuyongfa218 2009-07-02
  • 打赏
  • 举报
回复
使用find_first,findnext使用递归,列出所有文件。
wokao112358 2009-07-02
  • 打赏
  • 举报
回复
不过好像不符合题目要求,还是用递归吧
only_delusion 2009-07-02
  • 打赏
  • 举报
回复
2楼的 用的是什么函数? 感觉是调用系统命令阿
wokao112358 2009-07-02
  • 打赏
  • 举报
回复
2楼真是简明扼要,在下佩服,试了下,可以

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧