2个回答
展开全部
遍历文件夹,读取文档名,存到字符串中即可
更多追问追答
追问
如何遍历文件夹啦?
追答
#include
#include
#include
#include
#include
#include
int main()
{
DIR *dir;
struct dirent *direntp;
//打开目录,src为目录名
dir = opendir(src);
/*遍历目录*/
while((direntp = readdir(dir))!=NULL)
{
printf("%s\n", direntp->d_name);
//direntp->d_name即为文件名
}
return 0;
}
你可以参考这段代码
展开全部
思路基本就是遍历,递归是找到里面的文件夹里面的TXT文本!VS2008下编译 运行都OK!
#include <string>
#include <list>
#include <iostream>
#include <Windows.h>
using namespace std;
int myDelete(string path)
{
WIN32_FIND_DATA findData;
HANDLE hHandle = NULL;
string filePathName;
string fullPathName;
filePathName = path;
filePathName += "\\*.*";
list<string> pathList;
hHandle = FindFirstFile(filePathName.data(), &findData);
if ( hHandle == INVALID_HANDLE_VALUE )
{
return 1;
}
do
{
if ( "." == (string)findData.cFileName || ".." == (string)findData.cFileName )
{
continue;
}
fullPathName = path;
fullPathName += "\\";
fullPathName += findData.cFileName;
if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
myDelete(fullPathName.data());
}
else
{
//string str = fullPathName.substr(fullPathName.length() - 3); //截取文件名后3位
if ( fullPathName.substr(fullPathName.length() - 3) == "txt" )
{
pathList.push_back(fullPathName);
}
}
} while ( FindNextFile(hHandle, &findData) );
FindClose(hHandle);
list<string>::iterator pos;
for ( pos = pathList.begin(); pos != pathList.end(); pos++)
{
cout << *pos << " pos"<< endl;
}
/* list<string>::reverse_iterator rpos;
for ( rpos = pathList.rbegin(); rpos != pathList.rend(); rpos++ )
{
cout << *rpos << " rpos" << endl;
}*/
return 0;
}
int main()
{
string path = "F:\\a";
myDelete(path);
return 0;
}
#include <string>
#include <list>
#include <iostream>
#include <Windows.h>
using namespace std;
int myDelete(string path)
{
WIN32_FIND_DATA findData;
HANDLE hHandle = NULL;
string filePathName;
string fullPathName;
filePathName = path;
filePathName += "\\*.*";
list<string> pathList;
hHandle = FindFirstFile(filePathName.data(), &findData);
if ( hHandle == INVALID_HANDLE_VALUE )
{
return 1;
}
do
{
if ( "." == (string)findData.cFileName || ".." == (string)findData.cFileName )
{
continue;
}
fullPathName = path;
fullPathName += "\\";
fullPathName += findData.cFileName;
if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
myDelete(fullPathName.data());
}
else
{
//string str = fullPathName.substr(fullPathName.length() - 3); //截取文件名后3位
if ( fullPathName.substr(fullPathName.length() - 3) == "txt" )
{
pathList.push_back(fullPathName);
}
}
} while ( FindNextFile(hHandle, &findData) );
FindClose(hHandle);
list<string>::iterator pos;
for ( pos = pathList.begin(); pos != pathList.end(); pos++)
{
cout << *pos << " pos"<< endl;
}
/* list<string>::reverse_iterator rpos;
for ( rpos = pathList.rbegin(); rpos != pathList.rend(); rpos++ )
{
cout << *rpos << " rpos" << endl;
}*/
return 0;
}
int main()
{
string path = "F:\\a";
myDelete(path);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询