【问题标题】:saving a 2D array to a file c++将二维数组保存到文件 c++
【发布时间】:2014-04-06 20:27:36
【问题描述】:

我正在尝试将二维数组保存到文件中,但它显示为“^L”。如果用户输入 Y 或 y,则程序应该结束,但它正在打印我的二维数组。

//Allow user to quit
cout << "Would you like to quit this game? Enter Y or N: " << endl;
cin >> Choice;
if (Choice == 'Y' || Choice == 'y')
{
   ofstream outfile("Map.txt");
   for (int row = 0; row < ROWS; row++)
   {
       for (int col = 0; col < COLS; col++)
         outfile << "Here is your map! " << Map[ROWS][COLS] << endl;
   }
   outfile.close();
}

if (Choice == 'N' || Choice == 'n')
{
    // Add code to play the game
    PlayTurn(TreasureR, TreasureC, Row, Col, NumMoves);
}
// Print the map for true
PrintMap(Map,true, TreasureR, TreasureC, StartR, StartC, Row, Col);

//End the game
cout << "You finished the Game in  " << NumMoves <<" moves.\n";

【问题讨论】:

    标签: c++ arrays file multidimensional-array


    【解决方案1】:

    可以做这样的事情来将地图序列化为流。您可以指定流。 std::coutstd::fstream 可以工作..

    #include <iostream>
    #include <fstream>
    
    template<typename T, int height, int width>
    std::ostream& writemap(std::ostream& os, T (&map)[height][width])
    {
        for (int i = 0; i < height; ++i)
        {
            for (int j = 0; j < width; ++j)
            {
                os << map[i][j]<<" ";
            }
            os<<"\n";
        }
        return os;
    }
    
    int main()
    {
        const int width = 4;
        const int height = 5;
    
        int map[height][width] =
        {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 16},
            {17, 18, 19, 20}
        };
    
        std::fstream of("Map.txt", std::ios::out | std::ios::app);
    
        if (of.is_open())
        {
            writemap(of, map);
            writemap(std::cout, map);
            of.close();
        }
    }
    

    上面会将地图写入文件以及屏幕..

    结果将是:

    1 2 3 4 
    5 6 7 8 
    9 10 11 12 
    13 14 15 16 
    17 18 19 20 
    

    【讨论】:

      【解决方案2】:

      你所做的就是序列化。

      你有两个选择。

      1. 使用 boost 之类的库为您执行此操作
      2. 让你拥有

      因为你的要求很简单,我会自己做。

      std::ostream& serialize(std::ostream& outfile, int** arr, int rows, int cols) {
          outfile << rows << " ";
          outfile << cols << " ";
          for (int i = 0; i < rows; i++)
              for(int j = 0; j < cols; j++)
                  outfile << arr[i][j] << " ";
          return outfile;
      }
      
      int** deserialize(std::istream& file, int& rows, int& cols) {
          file >> rows;
          file >> cols;
          int** arr = new int*[rows];
          for (int i = 0; i < rows; i++) {
              arr[i] = new int[cols];
              for(int j = 0; j < cols; j++)
                  file >> arr[i][j];
          return arr;
      }
      

      此代码未经编译或测试!

      【讨论】:

      • 还有其他方法吗?我还没有在我的编程课上学习序列化。
      • @savannaalexis 这没什么。序列化只是意味着将数据结构存储到文本文件中。
      • 我只是不认为这就是我的教授想要的。我们才刚刚了解文件。
      • @savannaalexis 没有其他方法可以将数组保存到文件中。您必须迭代数组并保存数组中的所有值。
      猜你喜欢
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多