(C ++)从.dat文件创建二维数组

时间:2016-03-19 22:26:12

标签: c++ arrays converter

我正在尝试定义一个class CDTable,它有一个变量double table[4][4000](4乘4000的双精度数组)和一些根据表格进行计算的函数。

我的CDTable.h看起来像:

#define CDTABLE_H
class CDTable {
public:
    double table[4][4000];
    CDTable(string fileName);
    double dLookUp(double z);
};

我的CDTable.cpp的一部分看起来像:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <ctime>
#include <stdlib.h>
#include "CDTable.h"

using namespace std;

CDTable::CDTable(string fileName) {
    fstream file;
    file.open(fileName.c_str(), ios::in);
    string line;
    int col = 0;
    int row = 0;

    getline(file, line);    //skip the title line
    while (getline(file, line, '\t')) { //for each line
        istringstream ss(line);
        double val;
        while (ss >> val) {     //read the value
            table[row][col] = val;  //assign the value
            ++col;  //next column in the table
        }
        ++row;  //next line in the table
        col = 0;    //reset column
    }

    file.close();
}

int main() {
    CDTable cd = CDTable("CDLookUp.dat");
    return 0;
}

所以,我的意思是通过给它一个CDTable来构造一个string fileName对象,这是一个二维数组的.dat文件(.dat文件的第一行不是真实数据而是标题线)。我也有其他相关的方法在CDTable.cpp中实现,但我没有包含它们,因为它们似乎不是问题。

CDTable.cpp编译没有任何错误,但是当我运行它时,我得到分段错误(核心转储)。

有人能告诉我这里有什么问题吗?有没有更好的方法将.dat或.txt二维数据转换为C ++中的二维二维数组?

0 个答案:

没有答案