saving a 2D array to a file c++ -


i trying save 2d array file, showing "^l". if user enters y or y, program supposed end, printing 2d array instead.

//allow user quit cout << "would quit game? enter y or n: " << endl; cin >> choice; if (choice == 'y' || choice == 'y') {    ofstream outfile("map.txt");    (int row = 0; row < rows; row++)    {        (int col = 0; col < cols; col++)          outfile << "here map! " << map[rows][cols] << endl;    }    outfile.close(); }  if (choice == 'n' || choice == 'n') {     // add code play game     playturn(treasurer, treasurec, row, col, nummoves); } // print map true printmap(map,true, treasurer, treasurec, startr, startc, row, col);  //end game cout << "you finished game in  " << nummoves <<" moves.\n"; 

can serialize map stream. stream can specified you. std::cout or std::fstream can work..

#include <iostream> #include <fstream>  template<typename t, int height, int width> std::ostream& writemap(std::ostream& os, t (&map)[height][width]) {     (int = 0; < height; ++i)     {         (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();     } } 

the above write map file screen..

the result be:

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

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -