c++(9):c++按行读取文本文件代码及讲解

1 说明

        数据文件(以.dat,.txt等结尾的文本类文件)按行排列,每一行都是相同的类型,如下所示是多行位姿(x, y, z, qx, qy, qz, qw)数据。

2 c++程序

#include <fstream>//ifstream读文件,ofstream写文件,fstream读写文件
#include<iomanip>
#include <string>//文本对象,储存读取的内容
#include <iostream>//屏幕输出cout
#include <cstdlib>//调用system("pause");
using namespace std;

class node {//节点的数据格式

public:
  int origin_x;
  int origin_y;
  int halfDimension_x;
  int halfDimension_y;
  int depth;
  int type;
};

class mapheader {//地图的基本信息
    public:
        float latitude;
        float longitude;
        float qx;//初始姿态,四元数
        float qy;
        float qz;
        float qw;
        int map_width;//地图的宽度高度
        int map_height;       
        float map_resolution;//分辨率
        int origin_x;//为了尽可能使地图充满栅格所平移的距离
        int origin_y;
        int x_offset;//地图初始化时原点相对坐标系位置
        int y_offset;
};

class posedata {//每一帧点云的位姿
public:
  double UTME;
  double UTMN;
  double altitude;  
  double orientation_x;  
  double orientation_y;  
  double orientation_z;  
  double orientation_w;        
};


int main()
{

    /*  按行读取文件:
    ifstream in("/home/xxx/map.txt");
    string line;
    if(in) // 有该文件
    {
        while (getline (in, line)) // line中不包括每行的换行符
        { 
            cout << line << endl;
        }
    }
    else // 没有该文件
    {
        cout <<"no such file" << endl;
    }
    */


    // 参考链接:https://blog.csdn.net/ccc369639963/article/details/122941448
    
/*
    ifstream inFile;
    // inFile.open("/home/xxx/map_data.dat",ios::in|ios::binary); //二进制读方式打开

    if(!inFile) {
        cout << "error" <<endl;
        return 0;
    }
    node node_tmp;
    int line_count=0;
    while(inFile.read((char *)&node_tmp, sizeof(node_tmp))) { //一直读到文件结束
        line_count++;
        cout << node_tmp.origin_x << "--" << node_tmp.origin_y << "--" \
                  << node_tmp.halfDimension_x << "--" << node_tmp.halfDimension_y << "--" \
                  << node_tmp.depth << "--" << node_tmp.type << endl;                               
    }
    inFile.close();
    std::cout<<"文件共 "<<line_count <<"行."<<std::endl;
*/        


/*
    ifstream inFile;
    inFile.open("/home/xxx/map1.dat",ios::in|ios::binary); //二进制读方式打开
    if(!inFile) {
        cout << "error" <<endl;
        return 0;
    }
    mapheader mapheader_tmp;
    while(inFile.read((char *)&mapheader_tmp, sizeof(mapheader_tmp))) { //一直读到文件结束

        // cout <<setprecision(20)<< mapheader_tmp.latitude << "  " <<setprecision(20)<< mapheader_tmp.longitude << "  " 
        cout << mapheader_tmp.latitude << "  " << mapheader_tmp.longitude << "  " \
                  << mapheader_tmp.qx << "  " << mapheader_tmp.qy << "  " \
                  << mapheader_tmp.qz << "  " << mapheader_tmp.qw << "  "\
                  << mapheader_tmp.map_width << "  " << mapheader_tmp.map_height << "  "<< mapheader_tmp.map_resolution<<"  "\
                  << mapheader_tmp.origin_x << "  " << mapheader_tmp.origin_y <<"  "\    
                  << mapheader_tmp.x_offset << "  " << mapheader_tmp.y_offset << endl;                                   
    }
    inFile.close();
*/

    ifstream inFile;
    inFile.open("/home/xxx/pose.dat",ios::in|ios::binary); //二进制读方式打开
    if(!inFile) {
        cout << "error" <<endl;
        return 0;
    }
    posedata posedata_tmp;
    int line_count=0;
    while(inFile.read((char *)&posedata_tmp, sizeof(posedata_tmp))) { //一直读到文件结束
        line_count++;
        // cout <<setprecision(20)<< mapheader_tmp.latitude << "  " <<setprecision(20)<< mapheader_tmp.longitude << "  " 
        cout << posedata_tmp.UTME << "  " << posedata_tmp.UTMN << "  " \
                  << posedata_tmp.altitude << "  " << posedata_tmp.orientation_x << "  " \
                  << posedata_tmp.orientation_y << "  " << posedata_tmp.orientation_z << "  "\
                  << posedata_tmp.orientation_w << endl;                                   
    }
    inFile.close();
    std::cout<<"文件共 "<<line_count <<"行."<<std::endl;
    return 0;

}

编译与运行:

g++ xx.cpp

./a.out

猜你喜欢

转载自blog.csdn.net/BIT_HXZ/article/details/128933383