C++读取三维坐标文件

请教一下,文件类型为data,使用c++读取该文件,删去xyz并输出为txt格式

img

运行结果:

img

data文件中的数据用空格或者TAB分隔都是可以的。C++代码如下:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    ifstream infile("MeasureData_sws.data");
    ofstream outfile("out.txt");

    if (!infile.is_open())
    {
        cout << "文件打开失败" << endl;
        return 0;
    }
    double x, y, z;
    char ch;
    //读文件
    while (!infile.eof())
    {
        //逐行读取
        infile >> ch >> x >> ch >> y >> ch >> z;
        
        outfile <<fixed<<setprecision(6) << x << " " << y << " " << z << endl; //保留小数点后6位
    }
    infile.close();
    outfile.close();
    return 0;
}



用fopen打开,fscanf读取会比较容易一些
X、Y和Z之间间隔符号是一个空格吗?

int main()
{
    char s[1000] = {0};
    char x,y,z;
    double fx,fy,fz;
    FILE *fp = NULL;
    fopen_s(&fp,"MeasureData_sws.data","r");
    if(fp == NULL)
    {
        cout<<"file open error!";
        return 0;
    }
    FILE *fp1 = NULL;
    fopen_s(&fp1,"data.txt","w");
    if(fp1 == NULL)
    {
        cout<<"file open error!";
        return 0;
    }
    while(fgets(s,1000,fp) != NULL)
    {
        sscanf_s(s,"%c%lf  %c%lf  %c%lf",&x,&fx,&y,&fy,&z,&fz);
        fprintf(fp1,"%lf %lf %lf\n",fx,fy,fz);
    }
    fclose(fp);
    fclose(fp1);
}

#include <iostream>
#include <fstream>

int main()
{
    std::ifstream in_file("MeasureData_sws.data");
    std::ofstream out_file("output.txt");
    int no;
    double x, y, z;
    char ch;
    while (in_file >> no >> ch >> x >> ch >> y >> ch >> z)
        out_file << no << " " << x << " " << y << " " << z << '\n';
    return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632