Hellou. Can anyone tell me how can i save a string to a file?
I have this function:

void PrintGameBoard(jogo_4line*jg)
{
int i,n;

system("clear");
printf("===============Jogo do 4 em linha===============\n\n\n");

printf("    +--+--+--+--+--+--+--+\n");
for(i=1; i<=6; i++) 
    {
    printf("%d   |%c |%c |%c |%c |%c |%c |%c |\n",7-i,jg->T[i][1],jg->T[i][2],jg->T[i][3],jg->T[i][4],jg->T[i][5],jg->T[i][6],jg->T[i][7]);
    if (i!=0) printf("    +--+--+--+--+--+--+--+\n");
    }
printf("   ");
for (n=1;n<=7;n++) printf("  %d",n);
printf("\n");


}

i need to save the contents of this function to a file. I mean, this fucntion prints the state of a game 4 in a row in my program.

Then i have this function:

int SaveGame( jogo_4line *game, char *save1)
{
FILE *fp;
fp=fopen("save1","w");
char *str;

str=PrintGameBoard(game);
fprintf( fp,"%s\n", str);

fclose(fp);
return 1;

This is just a guess, obviously not working. Any clues on how to do this? thanks in advance

Recommended Answers

All 3 Replies

You can save it any way you like. In Sudoku, we save the grid as an 81 char string, with no line breaks. It's hard to understand it from looking at it outside the game configuration, but it allows us to save a TON of still readable grids, in a relatively small space.

Your options are anything you like - ideally, it would be in a format that you could share games with others interested in the game. Is there a standard format that's used for this game? Try Googling and see if you find a popular format.

This is for a school work. I have to save it to txt file. I'm using ubuntu console to run the game in the command line. I don't know how to convert the 'X' and 'O' positions to string so i can make a load file later. If i convert only the positions to strings i think it will be easier to load afterwards.

SHOW me an example of what you want for output of a game, and I'll tell you how to get that output.

Ubuntu console is just fine, and I know it's for school, and if you knew how to do it, you wouldn't be asking about it, here.

No problem. I'm here to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.