c++计算txt点云文档中点的个数计算结果为0,请问是什么问题

c++计算txt点云文档中点的个数计算结果为0,请问是什么问题

#include<iostream>
#include<fstream>
#include<vector>

int main()
{
    //打开点云数据文件
    std::ifstream in_file("传送带上的碗.txt");

    //定义用于存储点云数据的向量
    std::vector<float>points;

    //如果文件打开成功
    if (in_file.is_open())
    {
        float x, y, z;

        //逐行读取文件中的点
        while (in_file >> x >> y >> z)
        {
            //将点添加到向量中
            points.push_back(x);
            points.push_back(y);
            points.push_back(z);
        }

        //关闭文件
        in_file.close();

        //计算点的数量
        int num_points = points.size() / 3;

        //输出读取的点的数量
        std::cout << "number of points " << num_points << " points." << std::endl;
    }
    else
    {
        std::cerr << "Filed to open file." << std::endl;
    }
    return 0;
}

img

19行的输入需要空白字符分隔。你的文件里是逗号分隔的。需要单独处理逗号

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/700772
  • 这篇博客也不错, 你可以看下将C++运行结果保存到txt中
  • 除此之外, 这篇博客: C++实现哈夫曼编码中的 将读入的文件编码,写到txt文件中 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • void Encode(char* data,HuffmanCode HC,int length)
    {
    	ofstream outfile;
    	outfile.open("code.txt");
    	for(int i = 0;i < strlen(data);++i)  // 依次读入数据,查找对应的编码,写入编码文件 
    	{
    		for(int j = 1;j <= length;++j)
    		{
    			if(data[i] == HC[j].data)
    			{
    				outfile<<HC[j].str;
    			}
    		}
    	}
    	outfile.close();
    	cout<<"the code txt has been written"<<endl;
    	cout<<endl;
    }