-1

我有一个文本文件,如下所示:

173865.385  444879.102  0.299
173864.964  444879.137  0.467
173864.533  444879.177  0.612
173864.113  444879.211  0.798
173863.699  444879.244  1.002
173863.27   444879.282  0.926
173862.85   444879.317  0.974
....
....
....(around 200000 rows)

我正在尝试将每一列放入一个数组中。现在我有这些cripts:

int ReadDataFromFile(double * DataList[] ,int DataListCount,string &FileName)
{
    ifstream DataFile;
    int CurrentDataIndex = 0;;
    DataFile.open(FileName.c_str(),ios::in);
    if(DataFile.is_open()==true)
    {
        char buffer[200];
        while(DataFile.getline(buffer,200))
        {
            string strdata;
            stringstream ss(buffer);
            for(int i =0;i<DataListCount;++i)
            {
                getline(ss,strdata,' ');
                DataList[i][CurrentDataIndex] = strtod(strdata.c_str(),NULL);
            }
            ++CurrentDataIndex;
        }
    }
    return CurrentDataIndex;
}


int _tmain(int argc, _TCHAR* argv[])
{
    double a[200000],b[200000],c[200000];
    double* DataList[] = {a,b,c};
    int DataCount = ReadDataFromFile(DataList,3,string("D:\\read\\k0_test.txt"));
    for(int i=0;i<DataCount;++i)
    {
        cout<<setw(10)<<a[i]<<setw(10)<<b[i]<<setw(10)<<c[i]<<endl;
    }
    system("pause");
    return 0;
}

但它总是告诉错误“溢出”。有没有其他方法可以解决这个问题?

4

2 回答 2

0
 double a[200000],b[200000],c[200000];

用完程序的所有堆栈空间,尝试使用std::vector(首选)或使用动态数组,它在堆上分配内存。

例如:(仅供a参考)

vector<double> a;
a.reserve(200000);

或者

vector<double> a(200000);

如果使用动态数组:

double* a = new double[200000];

完成使用后不要忘记释放内存:

delete [] a;

有关详细信息,请参阅STL 向量

于 2013-05-06T16:24:44.340 回答
0

2个解决方案:

移到_tmaindouble a[200000],b[200000],c[200000];之外,以便它们可以成为全局变量。

或者,

将 a,b,c 声明为:

double *a = new double[200000]; 
double *b = new double[200000]; 
double *c = new double[200000];

并且不要忘记通过以下方式释放它们delete[]

希望这可以帮助 :)

于 2013-05-06T16:30:57.667 回答