C++读取一个文件下所有文件

读取所有文件

#include<iostream>
#include<string>
#include<vector>
#include<io.h>
using namespace std;
void getFiles(string path, vector<string>&files)
{
	//文件句柄
	//千万不要用long,运行的时候会暴内存异常 要用intptr_t
	intptr_t hFile = 0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			if ((fileinfo.attrib &_A_SUBDIR))
			{
				//遇到文件夹跳进文件夹直到搜素到文件
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
			}

			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name));
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

接收数据

struct PCD
{
	PointCloud::Ptr cloud;
	std::string f_name;

	PCD() : cloud(new PointCloud) {};
};
void loadData(std::vector<std::string>&argv, std::vector<PCD, Eigen::aligned_allocator<PCD> > &models)
{
	std::string extension(".pcd");
	// Suppose the first argument is the actual test model
	for (int i = 0; i < argv.size(); i++)
	{
		std::string fname = argv[i];
		// Needs to be at least 5: .plot
		if (fname.size() <= extension.size())
			continue;

		std::transform(fname.begin(), fname.end(), fname.begin(), (int(*)(int))tolower);

		//check that the argument is a pcd file
		if (fname.compare(fname.size() - extension.size(), extension.size(), extension) == 0)
		{
			// Load the cloud and saves it into the global list of models
			PCD m;
			m.f_name = argv[1];
			std::cout << "m.name: " << m.f_name << endl;
			pcl::io::loadPCDFile(argv[1], *m.cloud);
			//remove NAN points from the cloud
			std::vector<int> indices;
			pcl::removeNaNFromPointCloud(*m.cloud, *m.cloud, indices);

			models.push_back(m);
		}
	}
}

int main()
{
	const char *files_path = "C:\\Users\\xx\\Desktop\\pcdtest";
	std::vector<string>files;
	getFiles(files_path, files);

	// Load data
	std::vector<PCD, Eigen::aligned_allocator<PCD> > data;
	
	loadData( files, data);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值