c++遍历某个路径下的所有文件


正确的代码如下,注意,路径需要用char*格式,否则会出现读不出来的错误。

findfirst函数文档如下:

_findfirst函数:long _findfirst(const char *, struct _finddata_t *);


第一个参数为文件名,可以用"*.*"来查找所有文件,也可以用"*.cpp"来查找.cpp文件。第二个参数是_finddata_t结构体指针。若查找成功,返回文件句柄,若失败,返回-1。


然后,_findnext函数:int _findnext(long, struct _finddata_t *);


第一个参数为文件句柄,第二个参数同样为_finddata_t结构体指针。若查找成功,返回0,失败返回-1。


最后:_findclose()函数:int _findclose(long);


int main()
{
	
	struct info *str1;
	struct info s;
	long Handle;
	struct _finddata_t FileInfo;
	char* dir = "samplepicture\\*.*";
	int count = 1;
	if ((Handle = _findfirst(dir, &FileInfo)) == -1L)
		printf("没有找到匹配的项目\n");
	else
	{
		string File = FileInfo.name;
		string FileName = c_PATH + File;
		char* Filename = (char*)FileName.data();
		cout << "第" << count << "个" << FileInfo.name << "文件" << endl;
		//str1 = ReadXML(Filename);

		//cout << s.name << "   " << s.xmin << "   " << s.xmax << "   " << s.ymin << "   " << s.ymax << endl;

		while (_findnext(Handle, &FileInfo) == 0)
		{
			count++;
			cout << FileInfo.name << endl;
			string File = FileInfo.name;
			string FileName = c_PATH + File;
			char* Filename = (char*)FileName.data();
			//cout << FileName << endl;
			cout << "第" << count << "个.xml文件" << endl;
		//	str1 = ReadXML(Filename);

			//	cout << s.name << "   " << s.xmin << "   " << s.xmax << "   " << s.ymin << "   " << s.ymax << endl;

		}

		_findclose(Handle);
	}
	cout << count << endl;
	//productXML();
	system("pause");

	return 0;
	
	
}

参考文献

[1].c++遍历某个路径下的所有文件.http://bbs.csdn.net/topics/390368005/
[2].用 _findfirst 和 _findnext 查找文件(windows可用) .http://blog.sina.com.cn/s/blog_56d8ea900100yejj.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值