暴简单的console程序怎么就不行呢?——怎么列出某一目录下的所有文件名?

Ashura 2001-12-25 11:09:21
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
CString strDir;
CFileFind Finder;
BOOL bWorking;
CString strFixedFile = "*.*";
CString strFileName;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
printf((LPCSTR)"Please enter the directory:\n");
scanf("%s", strDir);
strDir += _T('\\');
strDir += strFixedFile;
printf((LPCSTR)strDir);
bWorking = Finder.FindFile(strDir);
while( bWorking )
{
bWorking = Finder.FindNextFile();
if ( Finder.IsDots() )
continue;
strFileName = Finder.GetFileName();
printf((LPCSTR)strFileName);
}
}

return nRetCode;
}

一个如此简单的打印某一目录下所有文件名的程序怎么就没有正确结果?
不管怎么给path,得到的总是d:盘下的一级子目录名,faint!
...全文
112 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
code_cold 2001-12-25
  • 打赏
  • 举报
回复
帮忙Up+Push
http://www.csdn.net/Expert/topic/439/439303.shtm
coolfired 2001-12-25
  • 打赏
  • 举报
回复
巨简单,改动如下:

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
//CString strDir;
char strDir[200]; //这里改了。


CFileFind Finder;
BOOL bWorking;
CString strFixedFile = "*.*";
CString strFileName;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
printf((LPCSTR)"Please enter the directory:\n");
scanf("%s", strDir);
//printf((LPCSTR)strDir);
//strDir += _T("\\");
//strDir += strFixedFile;
sprintf(strDir,"%s\\*.*",strDir); //这里更简单了。
printf((LPCSTR)strDir);

bWorking = Finder.FindFile(strDir);
while( bWorking )
{
bWorking = Finder.FindNextFile();
if ( Finder.IsDots() )
continue;
strFileName = Finder.GetFileName();
strFileName+="\n";
printf((LPCSTR)strFileName);
}
}

return nRetCode;
}
没问题了,原因:你用CString,'\'就不能表示正确的目录了。那FindFile就认为是NULL,你的程序在d:下,所以如此。FindFile第一个参数应该为"c:\\windows\*.*"之类的字符串。
hbs_biscuit 2001-12-25
  • 打赏
  • 举报
回复
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{

int nRetCode = 0;
CString strDir;
CFileFind Finder;
BOOL bWorking;
CString strFixedFile = "*.*";
CString strFileName;


// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
printf((LPCSTR)"Please enter the directory:\n");
char str[MAX_PATH];
scanf("%s", str);
strDir.Format("%s",str);
strDir += "\\";
strDir += strFixedFile;
printf("\n");
printf((LPCSTR)strDir);
printf("\n");
bWorking = Finder.FindFile(strDir);
while( bWorking )
{
bWorking = Finder.FindNextFile();
if ( Finder.IsDots() )
continue;
strFileName = Finder.GetFileName();
printf("\n%s",(LPCSTR)strFileName);
}

}

return nRetCode;
}
//尽量不要用操作c的函数操作CString

Ashura 2001-12-25
  • 打赏
  • 举报
回复
kao,麻烦哪位好心的兄弟帮偶run一把,感激不尽!
kingfish 2001-12-25
  • 打赏
  • 举报
回复
char strbuf[256];
scanf("%s", strbuf);
strDir = strbuf;
kingfish 2001-12-25
  • 打赏
  • 举报
回复
不好意思,上面写错了,
strDir += _T('\\'); // 这句有问题
luckyegg 2001-12-25
  • 打赏
  • 举报
回复
if ( Finder.IsDots() )有问题呀.把strFileName = Finder.GetFileName();放到if前面,应判断strFileName是不是目录,是则continue,不是则打印
kingfish 2001-12-25
  • 打赏
  • 举报
回复
strDir += _T('\\*.*');
Ashura 2001-12-25
  • 打赏
  • 举报
回复
ok!

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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