Specify a file name to save

I'm creating a Sudoku game, and would like to save to board contents to a file. I have it to work with a known file name, but would like to be able to specify a file name, to save the data to.

Here is the routine I'm trying to use for loading, but it's not compiling. Could someone let me know where, or what, the problem is??

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
void Save_Game(int sudoku_board[][9], int start_board[][9])
{
	int i,j;
	string File_Name,Sudoku_Name;
	//ofstream outfile("C:\\Users\\whitenite1\\Documents\\Visual Studio 2008\\Projects\\Sudoku_Game\\Sudoku_game.dat");
// Loads if I use the above, instead of the asking for file name section, below
	
	gotoXY(20,15, "Please give the file, a name.."); 
	cin >> Sudoku_Name;
	File_Name = "C:\\Users\\whitenite1\\Documents\\Visual Studio 2008\\Projects\\Sudoku_Game\\" + Sudoku_Name + ".dat";
	
	ofstream outfile(File_Name);
	
	for (i=0;i<9;i++)
	{
		for ( j=0;j<9;j++)
		{
			outfile << sudoku_board[j][i] << " ";
		}
		outfile << endl;
	}
	outfile << endl;
	for (i=0;i<9;i++)
	{
		for ( j=0;j<9;j++)
		{
			outfile << start_board[j][i] << " ";
		}
	   outfile << endl;
	}
		outfile.close();
}
Yeah, fstreams can work with cstrings not strings, you need to convert a string to a cstring:

1
2
3
4
5
6
// ...
cout << "Save to: ";
cin >> Sudoku_Name;
// ...
ofstream outfile(Sudoku_Name.c_str());
// ... 

@ToniAz

Thanks. I tried it out, and the file saved. Of course.. ;) So, this //ofstream outfile("C:\\Users\\whitenite1\\Documents\\Visual Studio 2008\\Projects\\Sudoku_Game\\Sudoku_game.dat"); is then a cstring? Because it saved as is. One last question, if I may. I am wanting the user to be able to see all the .dat files in the directory, so one can be chosen to be loaded into the grid. Is finding the files as easy as the saving, was?
Yes, string literals are const char*. Keep in mind that you could construct an std::string with them.
Next time post the compiler messages.

Managing directories is system dependant.
Check out dirent.h your API

Or you could go further and use a database.
Last edited on
Check out dirent.h your API

Will do. I found the dirent.h file and DL'ed it. Guess I'll try to use a vector for the file names, and scroll through them for the user's choice.

And thanks for the info on the string literals.

Again, thanks to both of you for your help.
Topic archived. No new replies allowed.