虽然是不难的问题,但也弄了很久,两天的时间都纠结在原来很简单的问题上,也许变成就是这样,一个小小的问题会让整个大程序都运行不出结果,真是让人无比郁闷!
最终测试正确的代码:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
char *filepath="D:\\VSprogram\\pcd\\pcd\\XexpandResult3D .txt"; //路径要为绝对路径,而且是双斜杠
double array[27665][3]={NULL}; //还是赋予准确的行数比较好,不然会乱
FILE *fp;
int main (int argc, char** argv)
{
if((fp=fopen(filepath,"r"))==NULL)
{ printf("can not open %s",filepath);
}
pcl::PointCloud<pcl::PointXYZ> cloud;
cloud.width = 27665;
cloud.height = 1;
cloud.is_dense = false;
cloud.points.resize (cloud.width * cloud.height);
while(!feof(fp)) //判断是否到了文件结尾
{
for (size_t m=0;m<27665;++m)
{
fscanf(fp,"%lf %lf %lf",&array[m][0],&array[m][1],&array[m][2]);
cloud.points[m].x = array[m][0];
cloud.points[m].y = array[m][1];
cloud.points[m].z = array[m][2];
}
}
fclose(fp);
pcl::io::savePCDFileASCII ("mypointcloud1.pcd", cloud);
std::cerr << "already save " << cloud.points.size () << " data points to mypointcloud1.pcd" << std::endl;
return (0);
}
最终生成了想要的pcd文件