Click here to Skip to main content
15,436,765 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey !! i am doing tic tac toe game using classes and i am completely done with it but there is problem with it using the save game and load game function i am getting error in save game , i just want to save game in binary file using file i/o and i read about it, but i couldn't found enough about it !! i want to ruin some of your precious time, (Don't Mind) !! i am stuck in it need urgent help !!here is what i do

What I have tried:

#include<iostream>
#include<fstream>



//#include<cstdlib>
using namespace std;
class tic
{
private:
	static char table[];//static variable initilization require so last me initilize kiya hua hay class say bahar
	int i, choice;//local variables jo code me istimaal hon gay
	char spot;
	static char replay;//same oper wala case
	static int player;//same oper wala case

void board()//board bana rahy hein jahan pey hum isko khelna chahty hein
cout << "\t \t \t \t  Tic Tac Toe\t\t\n\n";
cout << "\t \t \t PLAYER 1(X)  ,  PLAYER 2(O)\n\n";
cout << "\t \t \t " << table[1] << "|" << table[2] << "|" << table[3] << endl;//array ki location zero pay jo value hogi usko insert kar day ga similarly agay bhi

void Save_Game(char board[3][3])
{
    int i;
    FILE *savegame = fopen("save.bin", "w");
    for (i = 0; i < 3; i++)
    {
        fprintf(savegame, "%c %c %c\n", board[i][0], board[i][1], board[i][2]);
    }
    fclose(savegame);
    cout << "game saved";
}

int win()
{
    if (table[1] == table[2] && table[2] == table[3])
        return 1;

    else if (table[4] == table[5] && table[5] == table[6])
        return 1;
//////////
Posted
Updated 8-Jan-17 1:47am

Start by examining the file you have created in a text or hex editor and looking to see exactly what you have written.
With luck, it should look like this:
X O .
O X O
X . O
But if it doesn't, then you need to look at exactly why.
The next thing to do is to look at how you read the file back in: you don't show that, but do remember that since you write spaces and newlines to the file, they will be present when you read the file back, and you will have to remove and cope with those.

And don't use the default folder for save games: if this ever got into production, the folder would be in Program Files and access to that is severely restricted to reduce virus activity. Use a folder that is related to the user if possible and that has read/write permissions for the relevant users.
 
Share this answer
 
Comments
mayashah 8-Jan-17 9:32am    
for saving what do i have to do in the save function !!!
OriginalGriff 8-Jan-17 10:04am    
I don't know - you have code that looks like it works, so what does it do that you didn't expect, or not do that you did?
mayashah 8-Jan-17 11:19am    
ohh sorry i want to ask how could i load it ?? i mean can u guide me towards load function ??
OriginalGriff 8-Jan-17 11:35am     CRLF
Have you looked at the fscanf function? But me? I'd save it without the spaces or newlines to make it easier.
mayashah 8-Jan-17 11:43am    
how could you it could ease me with the save and load !!!
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
mayashah 8-Jan-17 9:31am    
you only have a that answer to every problem !! dont tease yourself posting that kind of stuff !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900