1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
void saveFile (int board[][9], char saveGame[])
{
ofstream fout (saveGame);
if (fout.fail ())
{
perror("ERROR: ");
return;
}
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
fout << board[row][col] << " ";
}
fout << '\n';
}
}
void readFile (char fileName[], int board[][9])
{
ifstream fin (fileName);
if (fin.fail ())
{
perror("Error");
return;
}
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
fin >> board[row][col];
}
}
}
void printBoard (int board[9][9])
{
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
cout << board[row][col] << "\t";
}
cout << '\n';
}
}
int main ()
{
char fileName[256] = "Soduko.txt";
int board[9][9] =
{
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
};
cout << "Going to save the board to " << fileName << "\n";
saveFile (board, fileName);
cout << "\nGoing to read the board from " << fileName << "\n";
readFile (fileName, board);
cout << "Board data read: \n\n";
printBoard (board);
//system ("pause");
return 0;
}
|