|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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[6];
- int ncols, nrows, cellsize, NODATA_value;
- double xllcorner, yllcorner;
- manning >> name[0] >> ncols;
- manning >> name[1] >> nrows;
- manning >> name[2] >> xllcorner;
- manning >> name[3] >> yllcorner;
- manning >> name[4] >> cellsize;
- manning >> name[5] >> NODATA_value;
- double **array = new double *[nrows];
- for(int i = 0; i < nrows; ++i) {
- array[i] = new double[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[y][x];
- }
- }
- // print
- std::cout << name[0] <<" " << ncols << std::endl;
- std::cout << name[1] << " " << nrows << std::endl;
- std::cout << name[2] << " " << xllcorner << std::endl;
- std::cout << name[3] << " " << yllcorner << std::endl;
- std::cout << name[4] << " " << cellsize << std::endl;
- std::cout << name[5] << " " << NODATA_value << std::endl;
- /*for(int y = 0; y < nrows; ++y) {
- for(int x = 0; x < ncols; ++x) {
- std::cout << array[y][x] << " ";
- }
- std::cout << std::endl;
- }*/
- for(int i = 0; i < nrows; ++i) {
- delete[] array[i];
- }
- delete[] array;
- manning.close();
- }
- #if 0
- 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("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[i][j] = atof(const_cast<const char*> (str.c_str()));
- array[i][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[i][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;
- }
- #endif
- int main()
- {
- test();
- return 0;
- }
复制代码
|
|