获得指定后缀名文件的文件名应该怎么写啊 ,急!!!希望高手帮帮小弟
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <stack>
#include <windows.h>
using namespace std;
void function( LPCTSTR lpszPath,ostream & out)
{
int a=0;
//开始查找;
stack<TCHAR*> Dirs;
stack<int> DirDepth;
TCHAR *tmp=new TCHAR[lstrlen(lpszPath)+1];
lstrcpy(tmp,lpszPath);
if(tmp[lstrlen(tmp)-1]=='\\')
tmp[lstrlen(tmp)-1]='\0';
TCHAR szFind[MAX_PATH*2];
TCHAR szFile[MAX_PATH*2];
TCHAR *curdir;
int curdepth=1; //当前文件夹的深度
Dirs.push(tmp);//压栈
DirDepth.push(curdepth);
out<<"[files]"<<endl;
for(;!Dirs.empty();)
{
curdir=Dirs.top();
curdepth=DirDepth.top();
Dirs.pop();
DirDepth.pop();
lstrcpy(szFind,curdir);
lstrcat(szFind, "\\*.*"); // 找所有文件
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(szFind, &wfd);
if (hFind != INVALID_HANDLE_VALUE) // 如果找到
{
if (curdepth >1)
out<<"";
for(int i=1;i<curdepth-1;++i)
{
out<<" ";
}
out<<endl;
do
{
if (wfd.cFileName[0] == '.')
continue;
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
wsprintf(szFile, "%s\\%s", curdir, wfd.cFileName);
TCHAR* tmp=new TCHAR[lstrlen(szFile)+2];
lstrcpy(tmp,szFile);
Dirs.push(tmp);
DirDepth.push(curdepth+1);
}
else
{
TCHAR *p;
a++;
p=curdir+strlen(curdir);
while(*p--!='\\');
while(*p=='\\') p--;
p+=2;
if (curdepth==1)
{
out<<"file"<<a<<" = ";
for(int i=1;i<curdepth;++i)
{
out<<"";
}
//TCHAR *q_bijiao;///////////////////就是在
TCHAR *q;////////////////////////////这里要修改
int length;//////////////////////////,但是不知道到底
length=strlen(wfd.cFileName)-1;//////该怎么搞啊
q=strchr(wfd.cFileName,length);//////
//length=strcspn(,q_bijiao);/////////
//q=strstr(wfd.cFileName,".");///////
if(q=="c")///////////////////////////
out<<wfd.cFileName<<endl;
}
else
{
out<<"file"<<a<<" = "<<p<<"\\";
out<<wfd.cFileName<<endl;
}
}
}
while (FindNextFile(hFind, &wfd));
}
delete [] curdir;
FindClose(hFind);
}
}
int main(int argc,char *argv[])
{
ofstream fout("遍历结果.txt");
if(argc<=1)
{
cerr<<endl<<"文件夹遍历,请输入路径:";
TCHAR path[MAX_PATH];
cin>>path;
// TCHAR *path1="E:\\SDK_CP\\youhuo\\MTK176X220\\images";
// TCHAR *path;
// path=path1;
function(path,fout);
}
else
{
function(argv[1],fout);
}
return 0;
}
生成的TXT文件中,格式如下:
在第一层文件夹下面只包含.c文件,其他类型的都不要,而且前面的序号是按顺序排下来的。
[files]
file1 = images\bar.bmp
file2 = images\begin.bmp
file3 = images\boss.bmp
file4 = images\bossmap.bmp
file5 = images\door.bmp
file7 = images\enemy.bmp
file9 = images\gameover1.bmp
file10 = images\gameover2.bmp
file11 = images\hand.bmp
file12 = images\jewel.bmp
file14 = images\life.bmp
file15 = images\main.bmp
file16 = images\mainmenu.bmp
file17 = images\map1.bmp
file18 = images\map5.bmp
file19 = images\map6.bmp
file20 = images\menu1.bmp
file21 = images\menu2.bmp
file22 = images\menu3.bmp
file23 = images\number.bmp
file24 = images\pause.bmp
file25 = images\pig.bmp
file26 = images\pigsha.bmp
file27 = images\player.bmp
file28 = images\select.bmp
file29 = images\shasheng.bmp
file30 = images\sign.bmp
file31 = images\softkey.bmp
file32 = images\time.bmp
file33 = images\win1.bmp
file34 = images\win2.bmp
file35 = images\yun.bmp
file38 = images\gameover3.bmp
file51 = GameTool.c
file52 = GameSound.c
file53 = encode.c
file54 = sendsms.c
file55 = GameCanvas.c
file72 = sounds\0.mid
希望高手帮我解答下