_findfirst为什么会返回-1

ycjunhua 2014-01-27 01:06:51
各位大神。
第一步我在调用文件对话框保存完文件后,代码如下:

我调用了文件对话框的代码
char buf[MAX_PATH] = {};

HWND hwnd = GetActiveWindow();
OPENFILENAME ofn = {};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = buf;
ofn.nMaxFile = MAX_PATH;

if (IDOK == GetSaveFileName(&ofn))
{
wr.Create();
char tmap[_MAP_S];
for (int i = 0;i < _MAP_S; ++i)
{
int cx = i % _MAP_W;
int cy = i / _MAP_W;
char* q = ag.GetGrid(cx, cy);
tmap[cx + cy * _MAP_W] = *q;
}

wr.WriteString(tmap);
wr.Write(buf);
}
第二步点击列表框得到一条选中的文件名,再遍历一个目录下的所有文件得到这个文件。

int CGameScene::insertDirectory( const char* dn,std::vector<std::string>* list )
{
int r = 0;

std::string s1 = dn;
s1 += "\\*.*";

_finddata_t fd;
intptr_t fr = _findfirst(s1.c_str(), &fd);
if (-1 != fr)
{
do
{
//子目录
if (fd.attrib & _A_SUBDIR)
{
if (strcmp(fd.name, ".") != 0 && strcmp(fd.name, "..") != 0)
{
std::string s2 = dn;
s2 += "\\";
s2 += fd.name;
r += insertDirectory(s2.c_str(),list);
}
}
//文件
else
{
//检查是否为位图文件
char* p = strrchr(fd.name, '.');
if (p)
{
//得到后缀名
std::string s3 = p + 1;

//将所有的字符归于小写
for (unsigned int i = 0; i < s3.length(); ++i)
{
if (s3[i] >= 'A' && s3[i] <= 'Z')
s3[i] += ('a' - 'A');
}

//确实为位图文件
if (strcmp(s3.c_str(), "map") == 0)
{
std::string s4 = "";
//s4 += "\\";
s4 += fd.name;
r += 1;
list->push_back(s4);
}
}
}
} while (0 == _findnext(fr, &fd));
_findclose(fr);
}
return r;

}

intptr_t fr = _findfirst(s1.c_str(), &fd);就返回-1了,我不调用第一段的代码,单独调用第二段的代码就能返回非-1.请问各位大神怎么解决这问题。
...全文
1278 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycjunhua 2014-10-29
  • 打赏
  • 举报
回复
c++有个获取工程路径的函数getwd,加上路径就ok了
赵4老师 2014-01-27
  • 打赏
  • 举报
回复
_findfirst, _findfirsti64, _wfindfirst, _wfindfirsti64 Provides information about the first instance of a filename that matches the file specified in the filespec argument. long _findfirst( char *filespec, struct _finddata_t *fileinfo ); __int64 _findfirsti64( char *filespec, struct _finddata_t *fileinfo ); long _wfindfirst( wchar_t *filespec, struct _wfinddata_t *fileinfo ); __int64 _wfindfirsti64( wchar_t *filespec, struct _wfinddata_t *fileinfo ); Function Required Header Compatibility _findfirst <io.h> Win 95, Win NT _findfirsti64 <io.h> Win 95, Win NT _wfindfirst <io.h> or <wchar.h> Win NT _wfindfirsti64 <io.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value If successful, _findfirst and _wfindfirst return a unique search handle identifying the file or group of files matching the filespec specification, which can be used in a subsequent call to _findnext or _wfindnext, respectively, or to _findclose. Otherwise, _findfirst and _wfindfirst return –1 and set errno to one of the following values: ENOENT File specification that could not be matched EINVAL Invalid filename specification Parameters filespec Target file specification (may include wildcards) fileinfo File information buffer Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tfindfirst _findfirst _findfirst _wfindfirst _tfindfirsti64 _findfirsti64 _findfirsti64 _wfindfirsti64 System Calls Routines | _find, _wfind Function Overview
xiaohuh421 2014-01-27
  • 打赏
  • 举报
回复
确认文件路径是否正确. 打印出lasterror看错误码
buyong 2014-01-27
  • 打赏
  • 举报
回复
const char* dn里面是什么内容
图灵狗 2014-01-27
  • 打赏
  • 举报
回复
遇到这种问题,单步调试是必须的。
引用 楼主 ycjunhua 的回复:
各位大神。 第一步我在调用文件对话框保存完文件后,代码如下: 我调用了文件对话框的代码 char buf[MAX_PATH] = {}; HWND hwnd = GetActiveWindow(); OPENFILENAME ofn = {}; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFile = buf; ofn.nMaxFile = MAX_PATH; if (IDOK == GetSaveFileName(&ofn)) { wr.Create(); char tmap[_MAP_S]; for (int i = 0;i < _MAP_S; ++i) { int cx = i % _MAP_W; int cy = i / _MAP_W; char* q = ag.GetGrid(cx, cy); tmap[cx + cy * _MAP_W] = *q; } wr.WriteString(tmap); wr.Write(buf); } 第二步点击列表框得到一条选中的文件名,再遍历一个目录下的所有文件得到这个文件。 int CGameScene::insertDirectory( const char* dn,std::vector<std::string>* list ) { int r = 0; std::string s1 = dn; s1 += "\\*.*"; _finddata_t fd; intptr_t fr = _findfirst(s1.c_str(), &fd); if (-1 != fr) { do { //子目录 if (fd.attrib & _A_SUBDIR) { if (strcmp(fd.name, ".") != 0 && strcmp(fd.name, "..") != 0) { std::string s2 = dn; s2 += "\\"; s2 += fd.name; r += insertDirectory(s2.c_str(),list); } } //文件 else { //检查是否为位图文件 char* p = strrchr(fd.name, '.'); if (p) { //得到后缀名 std::string s3 = p + 1; //将所有的字符归于小写 for (unsigned int i = 0; i < s3.length(); ++i) { if (s3[i] >= 'A' && s3[i] <= 'Z') s3[i] += ('a' - 'A'); } //确实为位图文件 if (strcmp(s3.c_str(), "map") == 0) { std::string s4 = ""; //s4 += "\\"; s4 += fd.name; r += 1; list->push_back(s4); } } } } while (0 == _findnext(fr, &fd)); _findclose(fr); } return r; } intptr_t fr = _findfirst(s1.c_str(), &fd);就返回-1了,我不调用第一段的代码,单独调用第二段的代码就能返回非-1.请问各位大神怎么解决这问题。

65,186

社区成员

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

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