C++ 读取 .txt 文件为 Eigen 库的 Matrix 数据格式,以及将其写入 .txt 文件

#include 
#include 
#include 
#include 
#include 
using namespace std;
using namespace Eigen;

MatrixXd openData(string fileToOpen)
{

    // the inspiration for creating this function was drawn from here (I did NOT copy and paste the code)
    // https://stackoverflow.com/questions/34247057/how-to-read-csv-file-and-assign-to-eigen-matrix

    // the input is the file: "fileToOpen.csv":
    // a,b,c
    // d,e,f
    // This function converts input file data into the Eigen matrix format

    // the matrix entries are stored in this variable row-wise. For example if we have the matrix:
    // M=[a b c
    //    d e f]
    // the entries are stored as matrixEntries=[a,b,c,d,e,f], that is the variable "matrixEntries" is a row vector
    // later on, this vector is mapped into the Eigen matrix format
    vector matrixEntries;

    // in this object we store the data from the matrix
    ifstream matrixDataFile(fileToOpen);

    // this variable is used to store the row of the matrix that contains commas
    string matrixRowString;

    // this variable is used to store the matrix entry;
    string matrixEntry;

    // this variable is used to track the number of rows
    int matrixRowNumber = 0;

    mRowNum = 0;

    while (getline(matrixDataFile, matrixRowString)) // here we read a row by row of matrixDataFile and store every line into the string variable matrixRowString
    {
        stringstream matrixRowStringStream(matrixRowString); // convert matrixRowString that is a string to a stream variable.

        int i = 0;
        while (getline(matrixRowStringStream, matrixEntry, ' ')) // here we read pieces of the stream matrixRowStringStream until every comma, and store the resulting character into the matrixEntry
        {
             matrixEntries.push_back(stod(matrixEntry)); // here we convert the string to double and fill in the row vector storing all the matrix entries
        }
        matrixRowNumber++; // update the column numbers
    }

    mRowNum = matrixRowNumber;

    // here we convet the vector variable into the matrix and return the resulting object,
    // note that matrixEntries.data() is the pointer to the first memory location at which the entries of the vector matrixEntries are stored;
    return Map>(matrixEntries.data(), matrixRowNumber, matrixEntries.size() / matrixRowNumber);
}

int main(int argc, char *argv[])
{
    Matrix trajectoryPoints;
    Matrix facets;
    if (argc < 2)
    {
        trajectoryPoints = openData("inputFile1.txt");
        facets = openData("inputFile2.txt");
    }
    else
    {
        trajectoryPoints = openData(argv[1]);
        facets = openData(argv[2]);
    }

    // 将 Matrix 格式数据保存到 .txt 文件
    std::ofstream outfile(argc < 3 ? "outputFile.txt" : argv[3]); // 创建输出文件流
    // 在mat.format()函数中,我们使用Eigen::IOFormat类指定了输出格式。
    // 其中,第一个参数表示精度,这里使用Eigen::StreamPrecision表示按照默认精度输出;
    // 第二个参数表示是否对齐列,这里使用Eigen::DontAlignCols表示不对齐;
    // 第三个参数表示矩阵元素之间的分隔符,这里使用空格;第四个参数表示行之间的分隔符,这里使用换行符;
    // 后面几个参数表示不需要特别指定
    outfile << trajectoryPoints.format(Eigen::IOFormat(Eigen::StreamPrecision, Eigen::DontAlignCols, " ", "\n", "", "", "", "")); // 将矩阵以txt格式写入文件
    outfile.close();                                                                                                           // 关闭文件流
    return 0;
}

你可能感兴趣的:(c++)