///[1]文件夹名 [2]后缀格式 [3]保存文件名的文件
///dataTxt txt flieName.txt
///实现同一文件夹下指定后缀名文件名字的获取,并将文件名字保存在txt文件中(不能有其他类型存在,否则有误)已经将第一行非文件名进行了剔除
///而且实现txtTopcd,但是将数据输出在了同一文件夹下,下一博客进行更改。存放于不同文件夹下
#include <io.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <io.h>
#include <string>
#include<windows.h>
using namespace std;
struct TXT_Point_XYZ
{
double x;
double y;
double z;
};
///第一步:获取文件夹内的某一类型 文件名并存放于txt文档中
void getAllFiles(string filePath, vector<string>& saveFileName, string postfix){
struct _finddata_t fileInfo;
long handle = 0;
string pathAndFileName;
if ((handle = _findfirst(pathAndFileName.assign(filePath).append("\\*").c_str(), &fileInfo)) != -1){
do{
if (strcmp(fileInfo.name, ".") != 0 && strcmp(fileInfo.name, "..") != 0){
string fileStr = fileInfo.name;
string prepostfix = "." + postfix;
int index = fileStr.find(prepostfix); ///在fileStr中查找子串prepostfix
///如果查找成功则输出查找到的第一个位置,否则返回-1;
string postStr = fileStr.substr(index, fileStr.size()); ///主要功能是复制子字符串,要求从指定位置开始,并具有指定的长度
if (0 == postStr.compare(prepostfix))
{
saveFileName.push_back(pathAndFileName.assign(filePath).append("/").append(fileInfo.name));
}
}
} while (_findnext(handle, &fileInfo) == 0);
_findclose(handle);
}
}
///第二步:实现txtToPcd文件格式的转换。
void txtToPcd(const char* readPath, const char* savePath, string file)
{
FILE* fp_txt;
TXT_Point_XYZ txt_points;
vector<TXT_Point_XYZ> my_vTxtPoints;
if ((fp_txt = fopen(readPath, "r")) != NULL)
{
while (fscanf(fp_txt, "%lf;%lf;%lf", &txt_points.x, &txt_points.y, &txt_points.z) != EOF)
{
/// fscanf()!=EOF的问题 EOF 是 end of file, 通常用于 读文件时,读到文件尾。文件尾有此符号。
my_vTxtPoints.push_back(txt_points);
}
}
pcl::PointCloud<pcl::PointXYZ> ::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
cloud->width = my_vTxtPoints.size();
cloud->height = 1;
cloud->is_dense = false;
cloud->points.resize(cloud->width*cloud->height);
for (int i = 0; i < cloud->points.size(); ++i)
{
cloud->points[i].x = my_vTxtPoints[i].x;
cloud->points[i].y = my_vTxtPoints[i].y;
cloud->points[i].z = my_vTxtPoints[i].z;
}
pcl::PCDWriter writer;
writer.write(savePath, *cloud, false);
}
int main(int argc,char** argv)
{
string basedir = argv[1]; ///基本目录
string postfix = argv[2]; ///后缀名||文件扩展名(filename extension)
vector<string> files;
getAllFiles(basedir, files, postfix);
string savingName = argv[3];
ofstream fout(savingName);
for (size_t i = 0; i < files.size(); i++)
{
fout << files[i] << endl;
}
fout.close();
std::cerr << "成功读取 " << files.size() << " 个文件" << std::endl;
for (size_t i = 0; i < files.size(); i++)
{
string pathtxt = files[i];
string pathpcd;
string str1 = "pcd";
string str2 = "txt";
string::size_type pos = files[i].find(str2);
if (pos != string::npos)
{
pathpcd = files[i].replace(pos, str2.size(), str1);
}
txtToPcd(pathtxt.c_str(), pathpcd.c_str(), files[i]);
}
std::cerr << "成功转换 " << files.size() << " 个文件" << std::endl;
system("pause");
return 0;
}
《PCL》实现txtTopcd,但是将数据输出在了同一文件夹下(一)
最新推荐文章于 2024-06-20 20:29:48 发布