鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 超级卡布达

[已解决]同样的代码从文件读数据,把数组换成用new定义的就出错了

[复制链接]
发表于 2020-4-29 00:28:47 | 显示全部楼层    本楼为最佳答案   
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4. #include<string>
  5. #include<sstream>

  6. #include<string.h>

  7. using namespace std;

  8. void test() {
  9.     ifstream manning("manning.asc", ios::in);
  10.     if (!manning.is_open()) {
  11.         cout << "文件打开失败" << endl;
  12.         exit(-1);
  13.     }

  14.     std::string name[6];
  15.     int ncols, nrows, cellsize, NODATA_value;
  16.     double xllcorner, yllcorner;
  17.     manning >> name[0] >> ncols;
  18.     manning >> name[1] >> nrows;
  19.     manning >> name[2] >> xllcorner;
  20.     manning >> name[3] >> yllcorner;
  21.     manning >> name[4] >> cellsize;
  22.     manning >> name[5] >> NODATA_value;

  23.     double **array = new double *[nrows];
  24.     for(int i = 0; i < nrows; ++i) {
  25.         array[i] = new double[ncols];
  26.     }

  27.     std::string line;
  28.     for(int y = 0; y < nrows; ++y) {
  29.         getline(manning, line);
  30.         std::stringstream ss(line);
  31.         for(int x = 0; x < ncols; ++x) {
  32.             ss >> array[y][x];
  33.         }
  34.     }

  35.     // print
  36.     std::cout << name[0] <<" " << ncols << std::endl;
  37.     std::cout << name[1] << " " << nrows << std::endl;
  38.     std::cout << name[2] << " " << xllcorner << std::endl;
  39.     std::cout << name[3] << " " << yllcorner << std::endl;
  40.     std::cout << name[4] << " " << cellsize << std::endl;
  41.     std::cout << name[5] << " " << NODATA_value << std::endl;
  42.     /*for(int y = 0; y < nrows; ++y) {
  43.         for(int x = 0; x < ncols; ++x) {
  44.             std::cout << array[y][x] << " ";
  45.         }
  46.         std::cout << std::endl;
  47.     }*/

  48.     for(int i = 0; i < nrows; ++i) {
  49.         delete[] array[i];
  50.     }
  51.     delete[] array;

  52.     manning.close();
  53. }

  54. #if 0
  55. void test()
  56. {
  57.     double** array = new double* [543];//array1 用来存储从gauges中读到的数据。
  58.     int i = 0, j = 0, line = 0;
  59.     for (i = 0; i < 543; i++)
  60.     {
  61.         array[i] = new double[220];
  62.     }

  63.     //double array[543][220];
  64.     //memset(array, 0, sizeof(array));

  65.     string lineStr;
  66.     //char* end;
  67.     ifstream manning;
  68.     manning.open("manning.asc", ios::in);
  69.     if (!manning.is_open())
  70.     {
  71.         cout << "文件打开失败" << endl;
  72.         exit(-1);
  73.     }

  74.     /*i = 0;
  75.     while (getline(manning, lineStr))
  76.     {
  77.         if (++line <= 6)
  78.             continue;
  79.         stringstream ss(lineStr);
  80.         string str;
  81.         j = 0;
  82.         while (getline(ss, str, ' '))
  83.         {
  84.             //array[i][j] = atof(const_cast<const char*> (str.c_str()));
  85.             array[i][j] = atof(str.c_str());
  86.             j++;
  87.         }
  88.         i++;
  89.     } */
  90.     i = 0;
  91.     while (getline(manning, lineStr))
  92.     {
  93.         if (++line <= 6)
  94.             continue;
  95.         stringstream ss(lineStr);
  96.         j = 0;
  97.         while( ss >> array[i][j++])
  98.             ;
  99.         i++;
  100.     }

  101.     manning.close();
  102.     for (int i = 0; i < 543; i++)
  103.     {
  104.         for (int j = 0; j < 220; j++)
  105.         {
  106.             cout << array[i][j] << " ";
  107.         }
  108.         cout << endl;
  109.     }

  110.     for (i = 0; i < 543; i++)
  111.     {
  112.         delete[] array[i];

  113.     }
  114.     delete[] array;
  115. }
  116. #endif

  117. int main()
  118. {
  119.     test();
  120.     return 0;
  121. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-29 00:30:00 | 显示全部楼层
  1. $ ./main
  2. ncols 543
  3. nrows 220
  4. xllcorner 488469
  5. yllcorner 3.79707e+06
  6. cellsize 2
  7. NODATA_value -9999
  8. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-29 00:31:26 | 显示全部楼层
  1. ncols         543
  2. nrows         220
  3. xllcorner     488469.3190918
  4. yllcorner     3797068.373291
  5. cellsize      2
  6. NODATA_value  -9999
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-29 14:41:09 | 显示全部楼层
5W的数据不算大,普通电脑跑应该没问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-29 22:36:44 | 显示全部楼层



                               
登录/注册后可看大图

你好,冒昧打扰,请问存这些数据的数组,我想判断数组中第一列的每一个数是不是50的整数倍。直接写
array[i][0] % 50 == 0 这种判断条件不行,我搜了一下 取余必须是整数,请问有什么其他的好的办法吗?数组定义是double,里面的数字也有整有零的,可能是1, 32.55, 50,  100.088  这种的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-29 23:17:28 | 显示全部楼层
超级卡布达 发表于 2020-4-29 22:36
你好,冒昧打扰,请问存这些数据的数组,我想判断数组中第一列的每一个数是不是50的整数倍。直接写
...

  1. https://www.baidu.com/s?wd=浮点数取余&pn=0&oq=浮点数取余&tn=48020221_10_hao_pg&ie=utf-8&rsv_idx=1&rsv_pq=99562fbe00041e21&rsv_t=51bdIOgBloxfHa2L%2FcwNooxLXsNZxALy0ASJUR6IXEf2kxgdzj4Xd5guDg9UTh2lYT5Tdf8Roj7q
复制代码

  1. https://zhidao.baidu.com/question/314258298.html?sort=11&rn=5&pn=0#wgt-answers
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-30 00:40:20 | 显示全部楼层

这样呀,好的。多谢多谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 14:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表