使用c++从.txt中读取第三个坐标

read from .txt the third coordinate using c++

本文关键字:三个 坐标 c++ txt 读取 使用      更新时间:2023-10-16

我有一个.txt文件,里面有点的真实坐标。场景是摄像机面对墙壁;在他们中间我有一个盒子。我想在.txt中只获取引用框的点,所以我想从坐标中读取第三个分量,意思是深度值,以及它是否大于某个距离来支撑所有的线。

file.txt

0.005545 0.065641.6354

0.235443 0.354642.6575

如果(值>2.5){从.txt中完全删除行}

所有的坐标都用一个空格隔开,线条用一个引子隔开。

感谢

我认为这会起作用:

#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
#define THRESH 2.5f
using namespace std;
int main()
{
    vector<float> DataArray;
    ifstream myfile("test.txt"); 
    copy(istream_iterator<float>(myfile),
         istream_iterator<float>(),
         back_inserter(DataArray));
    myfile.close();
    ofstream newfile("test.txt");
    for(int i = 2; i < DataArray.size(); i += 3)
    {
        if(DataArray[i] < THRESH)
        {
            for (int j = i-2; j <= i; ++j)
                newfile << DataArray[j] << "  ";
            newfile << endl;
        }
    }
    myfile.close(); 
    return 0;
}

使用std::ifstream读取文件。

std::string line;
std::ifstream input_file;
input_file.open("input_file.txt");
// Read the file one line at a time
while (std::getline(input_file, line))
{
    // Do your thing
}
input_file.close();

提示:写一个新文件,稍后重命名。