x64下vs2013 C++遍历目录下所有文件使用_findnext()调试时中断

来源:互联网 发布:软件设计师怎么考 编辑:程序博客网 时间:2024/06/01 08:26

今天尝试在VS2013 win32控制台下写一个手写数字的机器学习程序,其中需要遍历某目录下的所有.txt文件,在网上查了大量方法,主流是这样的

#include "stdafx.h"
#include<iostream>
#include<io.h>
#include<stdio.h>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
long Handle;
int k;
_finddata_t FileInfo;
if ((k = Handle =_findfirst("D:\\TestCode\\Machine-Learning-In-Action\\Chapter2\\digits\\testDigits\\*.txt", &FileInfo)) == -1)
{
cout << "没有找到匹配项目" << endl;
}
else
{
while (k != -1)
{
cout << FileInfo.name << endl;
k = _findnext(Handle, &FileInfo);
}
}
_findclose(Handle);
system("PAUSE");
return 0;
}

但调试运行时会出现的问题,如下



显然,上图为地址访问出错,

其实是因为_findfirst()返回类型为intptr_t而非long型,从“intptr_t”转换到“long”丢失了数据


因此仅需稍作改动即可


成功!

1 0
原创粉丝点击