获得指定后缀名文件的文件名应该怎么写啊 ,急!!!希望高手帮帮小弟

lijie8680293 2008-11-07 10:41:49
#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

希望高手帮我解答下

...全文
167 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lann64 2008-11-07
  • 打赏
  • 举报
回复
一般用字符串拼接上后缀
lijie8680293 2008-11-07
  • 打赏
  • 举报
回复
substr()我使用这个还是不行,还是有错误

Super.Jiju 2008-11-07
  • 打赏
  • 举报
回复
http://www.vckbase.com/document/viewdoc/?id=1060

返回的文件名放在vector中
自己判断一下就ok
Super.Jiju 2008-11-07
  • 打赏
  • 举报
回复
http://super-jiju.spaces.live.com/blog/cns!806C498DDEE76B61!492.entry


在获得文件名后;
只要substr(str.find("."),str.length()-str.find("."))就ok啦
lijie8680293 2008-11-07
  • 打赏
  • 举报
回复
这个只是获得文件的后缀名
我用strstr()也可以获得后缀名,
但是在比较要输出“.c”文件的时候,还是不行啊
newerC 2008-11-07
  • 打赏
  • 举报
回复

#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")

TCHAR szFileName[] = _T("xxxx.exe");
LPTSTR lpExt = PathFindExtension(szFileName); //返回.exe
jia_xiaoxin 2008-11-07
  • 打赏
  • 举报
回复
写了一个判断函数

#include <iostream> 
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <stack>
#include <windows.h>
using namespace std;

bool findSub(TCHAR * tchar)
{
int length = strlen(tchar);
for(int i = 0; i < length - 1; i++)
{
if(tchar[i] == '.' && (tchar[i+1] == 'c' || tchar[i+1] == 'C'))
return true;
}
return false;
}

int main(int argc, char* argv[])
{
TCHAR *A="file33 = images\\win1.c" ;
if(findSub(A))
cout << "find .c file ok" << endl;
else
cout << "no find .c file" << endl;

return 0;
}
lijie8680293 2008-11-07
  • 打赏
  • 举报
回复
那我现在要写一个字符串是否包含另一个字符串,比如:
TCHAR *A="file33 = images\win1.bmp"
TCHAR *B="file34 = images\win2.b"
TCHAR *C="file35 = images\yun.b"
TCHAR *D="file38 = images\gameover3.bmp
我现在要输出包含“.c”字符串的字符串,该怎么写

65,211

社区成员

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

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