【发布时间】:2013-05-06 16:19:32
【问题描述】:
我有一个如下所示的文本文件:
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)
我正在尝试将每一列放入一个数组中。 现在我有了这些脚本:
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.8 MB。改用
double *a = new double[200000]等(或者更好的是,使用向量!) -
另外,是“大约 200000 行”,还是“肯定少于 200000 行”,还是“可能超过 200000 行”?
-
我有几个文本文件,它们都有大约 200000 个条目。有些比这个大(但不是太多),有些比这个小。
标签: c++ arrays visual-c++