Sudoku Saving

Hey so I'm making a sudoku program for class and it works fine, but when I try saving it to a file and opening it again it wont work. I just need help figuring out how to get the save to correctly read again. Sorry I don't know to make the code stick out better. This is the savefile function and the main function that calls it when the user wants to save and quit.

void saveFile(int board[][9], char saveGame[])
{
cout << "What file would you like to write your board to: ";
cin >> saveGame;
ofstream fout(saveGame);
if(fout.fail())
cout << "ERROR";
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
fout << board[row][col];
}
}
fout.close();
cout << "Board written successfully" << "\n";
}


int main()
{
char fileName[256];
char saveGame[256];
int board[9][9];
getFileName(fileName);
writeFile(fileName, board);
int end = playGame(board);
if (end == 1)
saveFile(board, saveGame);
return 0;
}
Last edited on
Hi,
So what is the difference between writeFile() & saveFile()?

Where is your loadFile() function?
So getFile asks the user for the original sudoku setup and then write file puts it into an array.

void getFileName(char fileName[])
{
cout << "Where is your board located? ";
cin >> fileName;
}



void writeFile(char fileName[], int board[][9])
{
ifstream fin(fileName);
if (fin.fail())
{
cout << "Error";
}
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
fin >> board[row][col];
}
}

fin.close();
}
So you change the name "writeFile" to "readFile" please.

You are actually reading from a file but not writing to a file.
@LiLDorth
So please be more responsive. If you want your problems to be solved more quickly, you should really be more responsive.
Last edited on
The problem is not with reading the file. I can get the file and read from it and put the sudoku number into an array for the sudoku game to work and set integers for user guesses, but when I want to save the new set integers from user guess back into a file and then try to read it again it just has a bunch of random numbers
You try logging the game data. The more information the better.

1
2
3
4
5
6
7
8
9
10
11
12
13
ofstream fout(saveGame);
if(fout.fail())
cout << "ERROR";

cout << "Start writing..." << endl;
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
fout << board[row][col];
cout << "  " << board[row][col] << endl; 
}
}


And let us know the program output.
Last edited on
Try this:
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;
}
As Thomas1975 has pointed out in his example, you should really output a whitespace character between each two numbers :
fout << board[row][col] << ' ';
Does that help? :)
closed account 5a8Ym39o6 is right. The problem is that you need to print a space between the numbers. Otherwise. This code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <iostream>

int board[9][9];
using namespace std;


int main()
{
    for (int i=0; i<9; ++i) {
        for (int j=0; j<9; ++j) {
            board[i][j]=j;
        }
    }

    for (int i=0; i<9; ++i) {
        for (int j=0; j<9; ++j) {
            cout << board[i][j];
        }
    }
}


Produces this output:
012345678012345678012345678012345678012345678012345678012345678012345678012345678

And when you try to read it back, the whole string is interpretted as a single number.
That Worked! it was the space after it all. Thank you so much!
Glad it helped :)
Topic archived. No new replies allowed.