C++过滤指定文件夹中的文件

abc41106 2010-03-10 07:06:20
我想用C++遍历D盘的一个文件夹,比如“abc”这个文件夹,如果不存在则自动创建一个空的文件夹“abc”。
然后从中找出“.txt”文件,获得其文件名。该怎么处理?
请高手指教。
...全文
339 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
abc41106 2010-03-12
  • 打赏
  • 举报
回复
怎么没人回复呢?今天是我的最后期限了,我必须今天把这个问题解决,请大家帮帮我啊!
赵4老师 2010-03-12
  • 打赏
  • 举报
回复
system("cmd /c md d:\\abc");
system("cmd /c dir /a-d /b d:\\abc\\*.txt >d:\\abc_txtfiles.txt");
//读d:\\abc_txtfiles.txt的内容

记不得哪位C++大牛在哪本学习C++的书的前言里面说过
“用C语言1000行源码能完成的工作千万不要用C++重写!”

计算机组成原理→DOS命令→汇编语言→C语言、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言、架构……
abc41106 2010-03-11
  • 打赏
  • 举报
回复
遍历文件已经解决了,可是又有新问题了,我按照网上的提示,点击 工程--设置--Microsoft Foundation Classes 选项由Not using mfc 改为using mfc in a shalled dll
然后,在运行程序时没有出问题,但是当我关闭程序时,就会报错,说是引用内存地址错误,请问怎么解决啊?
bluemystery 2010-03-10
  • 打赏
  • 举报
回复
- -
已经够简单的了
bluemystery 2010-03-10
  • 打赏
  • 举报
回复
- -
已经够简单的了
abc41106 2010-03-10
  • 打赏
  • 举报
回复
有没有简单点的啊,上面的两个一个能勉强看懂,但不会用,而另一个根本看不懂,我是C++初学者,麻烦你们了
yuzl32 2010-03-10
  • 打赏
  • 举报
回复

#define _WIN32_WINNT 0x0501
#include <Windows.h>
#include <stdio.h>
#include <malloc.h>
#include <tchar.h>
#include <wchar.h>
#include <strsafe.h>

#define BUFSIZE MAX_PATH

int _tmain(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
LPTSTR DirSpec;
size_t length_of_arg;
INT retval;

DirSpec = (LPTSTR) malloc (BUFSIZE);

if( DirSpec == NULL )
{
printf( "Insufficient memory available\n" );
retval = 1;
goto Cleanup;
}


// Check for the directory to query, specified as
// a command-line parameter; otherwise, print usage.
if(argc != 2)
{
_tprintf(TEXT("Usage: Test <dir>\n"));
retval = 2;
goto Cleanup;
}

// Check that the input is not larger than allowed.
StringCbLength(argv[1], BUFSIZE, &length_of_arg);

if (length_of_arg > (BUFSIZE - 2))
{
_tprintf(TEXT("Input directory is too large.\n"));
retval = 3;
goto Cleanup;
}

_tprintf (TEXT("Target directory is %s.\n"), argv[1]);

// Prepare string for use with FindFile functions. First,
// copy the string to a buffer, then append '\*' to the
// directory name.
StringCbCopyN (DirSpec, BUFSIZE, argv[1], length_of_arg+1);
StringCbCatN (DirSpec, BUFSIZE, TEXT("\\*"), 2*sizeof(TCHAR));

// Find the first file in the directory.
hFind = FindFirstFile(DirSpec, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
_tprintf (TEXT("Invalid file handle. Error is %u.\n"),
GetLastError());
retval = (-1);
}
else
{
_tprintf (TEXT("First file name is: %s\n"),
FindFileData.cFileName);

// List all the other files in the directory.
while (FindNextFile(hFind, &FindFileData) != 0)
{
_tprintf (TEXT("Next file name is: %s\n"),
FindFileData.cFileName);
}

dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
_tprintf (TEXT("FindNextFile error. Error is %u.\n"),
dwError);
retval = (-1);
goto Cleanup;
}
}
retval = 0;

Cleanup:
free(DirSpec);
return retval;

}
最帅马老师 2010-03-10
  • 打赏
  • 举报
回复
仔细看看下边的代码吧,可能对你有帮助

void ScanDirectory(const CString &strDir)
{
CFileFind f;
CString strFilename;
CString strPath;

BOOL bFind = f.FindFile(strDir+"*.*");
while (bFind)
{
bFind = f.FindNextFile();
strFilename = f.GetFileName();

// if this is a directory
if (f.IsDirectory())
{
if (strFilename != "." && strFilename != "..")
{
strPath = strDir+strFilename;
strPath += '\\';

ScanDirectory(strPath);
}
}
else if (IsAExcelFile(strFilename))
{
// 可以在这里输出文件名
}
}

f.Close();
}

// 使用方法
ScanDirectory("C:\\");
abc41106 2010-03-10
  • 打赏
  • 举报
回复
能不能具体点啊?我对文件操作一点都不懂啊!
希望你能给点代码。
万分感谢!
yuzl32 2010-03-10
  • 打赏
  • 举报
回复
FindFirstFile, FindNextFile, and FindClose.

65,187

社区成员

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

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