#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;
}
|