马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
大家好,我想从.asc文件(纯数字)中读取数据,存到二维数组。
原来的代码如下:
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
void test()
{
double array[543][220];
memset(array, 0, sizeof(array));
int i = 0, j = 0, line = 0;
string lineStr;
char* end;
ifstream manning;
manning.open("C:\\Users\\lch\\Desktop\\C++\\MM\\manning.asc", ios::in);
if (!manning.is_open())
{
cout << "文件打开失败" << endl;
}
while (getline(manning, lineStr))
{
if (++line <= 6)
continue;
stringstream ss(lineStr);
string str;
j = 0;
while (getline(ss, str, ' '))
{
array[i][j] = atof(const_cast<const char*> (str.c_str()));
j++;
}
i++;
}
manning.close();
for (int i = 0; i < 543; i++)
{
for (int j = 0; j < 220; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
int main()
{
test();
system("pause");
return 0;
}
把数组用new开辟到堆区的话,在将数据转换成double存到数组的时候就出错了
这里代码提示错误→ array[i][j] = atof(const_cast<const char*> (str.c_str())); ←0x00007FF67438173A 处(位于 测试.exe 中)引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。
具体代码如下:
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
void test()
{
double** array = new double* [543];//array1 用来存储从gauges中读到的数据。
int i = 0, j = 0, line = 0;
for (i = 0; i < 543; i++)
{
array[i] = new double[220];
}
//double array[543][220];
memset(array, 0, sizeof(array));
string lineStr;
char* end;
ifstream manning;
manning.open("C:\\Users\\lch\\Desktop\\C++\\MM\\manning.asc", ios::in);
if (!manning.is_open())
{
cout << "文件打开失败" << endl;
}
while (getline(manning, lineStr))
{
if (++line <= 6)
continue;
stringstream ss(lineStr);
string str;
j = 0;
while (getline(ss, str, ' '))
{
array[i][j] = atof(const_cast<const char*> (str.c_str()));
j++;
}
i++;
}
manning.close();
for (int i = 0; i < 543; i++)
{
for (int j = 0; j < 220; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
for (i = 0; i < 543; i++)
{
delete[] array[i];
}
delete[] array;
}
int main()
{
test();
system("pause");
return 0;
}
请问大家这是什么原因呀?拜托大神帮忙看一下,非常感谢!
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<sstream>
#include<string.h>
using namespace std;
void test() {
ifstream manning("manning.asc", ios::in);
if (!manning.is_open()) {
cout << "文件打开失败" << endl;
exit(-1);
}
std::string name[zxsq-anti-bbcode-6];
int ncols, nrows, cellsize, NODATA_value;
double xllcorner, yllcorner;
manning >> name[zxsq-anti-bbcode-0] >> ncols;
manning >> name[zxsq-anti-bbcode-1] >> nrows;
manning >> name[zxsq-anti-bbcode-2] >> xllcorner;
manning >> name[zxsq-anti-bbcode-3] >> yllcorner;
manning >> name[zxsq-anti-bbcode-4] >> cellsize;
manning >> name[zxsq-anti-bbcode-5] >> NODATA_value;
double **array = new double *[zxsq-anti-bbcode-nrows];
for(int i = 0; i < nrows; ++i) {
array[zxsq-anti-bbcode-i] = new double[zxsq-anti-bbcode-ncols];
}
std::string line;
for(int y = 0; y < nrows; ++y) {
getline(manning, line);
std::stringstream ss(line);
for(int x = 0; x < ncols; ++x) {
ss >> array[zxsq-anti-bbcode-y][zxsq-anti-bbcode-x];
}
}
// print
std::cout << name[zxsq-anti-bbcode-0] <<" " << ncols << std::endl;
std::cout << name[zxsq-anti-bbcode-1] << " " << nrows << std::endl;
std::cout << name[zxsq-anti-bbcode-2] << " " << xllcorner << std::endl;
std::cout << name[zxsq-anti-bbcode-3] << " " << yllcorner << std::endl;
std::cout << name[zxsq-anti-bbcode-4] << " " << cellsize << std::endl;
std::cout << name[zxsq-anti-bbcode-5] << " " << NODATA_value << std::endl;
/*for(int y = 0; y < nrows; ++y) {
for(int x = 0; x < ncols; ++x) {
std::cout << array[zxsq-anti-bbcode-y][zxsq-anti-bbcode-x] << " ";
}
std::cout << std::endl;
}*/
for(int i = 0; i < nrows; ++i) {
delete[] array[zxsq-anti-bbcode-i];
}
delete[] array;
manning.close();
}
#if 0
void test()
{
double** array = new double* [zxsq-anti-bbcode-543];//array1 用来存储从gauges中读到的数据。
int i = 0, j = 0, line = 0;
for (i = 0; i < 543; i++)
{
array[zxsq-anti-bbcode-i] = new double[zxsq-anti-bbcode-220];
}
//double array[zxsq-anti-bbcode-543][zxsq-anti-bbcode-220];
//memset(array, 0, sizeof(array));
string lineStr;
//char* end;
ifstream manning;
manning.open("manning.asc", ios::in);
if (!manning.is_open())
{
cout << "文件打开失败" << endl;
exit(-1);
}
/*i = 0;
while (getline(manning, lineStr))
{
if (++line <= 6)
continue;
stringstream ss(lineStr);
string str;
j = 0;
while (getline(ss, str, ' '))
{
//array[zxsq-anti-bbcode-i][zxsq-anti-bbcode-j] = atof(const_cast<const char*> (str.c_str()));
array[zxsq-anti-bbcode-i][zxsq-anti-bbcode-j] = atof(str.c_str());
j++;
}
i++;
} */
i = 0;
while (getline(manning, lineStr))
{
if (++line <= 6)
continue;
stringstream ss(lineStr);
j = 0;
while( ss >> array[zxsq-anti-bbcode-i][j++])
;
i++;
}
manning.close();
for (int i = 0; i < 543; i++)
{
for (int j = 0; j < 220; j++)
{
cout << array[zxsq-anti-bbcode-i][zxsq-anti-bbcode-j] << " ";
}
cout << endl;
}
for (i = 0; i < 543; i++)
{
delete[] array[zxsq-anti-bbcode-i];
}
delete[] array;
}
#endif
int main()
{
test();
return 0;
}
|