I currently have a program that writes Sudoku boards to files; however, I would like the program to be able to read the board already in the file, copy it and then add another board to the file. It follows, then, that if there were two boards in the file, both boards would be copied and a third would be added to the file.

Currently, my code deletes the data already in the file and writes a new board to the file. I would like to remedy this. Can anyone suggest a function that can help solve this problem?

void WriteFile(int Board[9][9])
     ofstream SudokuBoard;
     SudokuBoard.open ("1.txt");
     for (int row = 0; row <= 8; row++)
          SudokuBoard << endl;
          for (int column = 0; column <= 8; column++)
              SudokuBoard << Board[column][row];  // prints horizontally. 

hmm... my function now looks like this:

SudokuBoard.open ("1.txt", fstream::ate | fstream::binary);

It puts a gigantic string of code in the file and removes the endl statements I previous used for spacing. In addition, old data is still written over. Any thoughts?

if you want to append to the end of file use ios::app...then do something like "/n" so that your data is written to the next line

ithen do something like "/n" so that your data is written to the next line

Actually it's '\n' (with a backslash) Typo I guess? ;)

>>SudokuBoard.open ("1.txt", fstream::ate | fstream::binary);

Two errors with the above:
1) fstream::ate should be ios::ate
2) If you want the file to be a text file then don't use the binary flag.

>>SudokuBoard << Board[column][row];
Already mentioned twice, but a third time won't hurt. You need to add "\n" to the end like this: SudokuBoard << Board[column][row] << "\n";

I ended up getting it to work using the fstream::app function. Thanks for the help guys.

This article has been dead for over six months. Start a new discussion instead.