tic tac toe into an array is right?

Write your question here.

My problem is why the my tictac toe board can´t looks like the original file.

The input file is:
X O . . . . . X .
This is the same thing as
X O .
. . .
. X .
Note that the . needs to be displayed as a space.

> Enter source filename: /home/cs124/assignments/assign40a.txt
> X | O |
> ---+---+---
>| |
> ---+---+---
> | X |
> Enter destination filename: /tmp/6b2b9755544c.txt
> File written

but when I retested this tic tac toe with the temporal I can see these results.

> Enter source filename: /tmp/6b2b9755544c.txt
> X | O | X \n
Exp: X | O | \n
> ---+---+---
> \0
Exp: | | \n
> | \0
Exp: ---+---+---\n
> | ▒ \n
Exp: | X | \n
> ---+---+---\n
Exp: Enter destination filename:
/tmp/6b2b9755544c.txt
> \0
Exp: File written\n
> | \0
Exp: No output


My code run when it is an unique test but when I try to use it again I see those results...really I feel tired to try with differents ways ,to resolve the output in the second test,If you can help me really I will apreciate it...

this is my code:

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

void readFile(char board[][9]);
void writeFile(char board[][9]);
void display(char board[][9]);

/**********************************************************************
* Main simple driver program
***********************************************************************/
int main()
{
   //Declare array
   char board[9][9];


   readFile(board);
   display(board);
   writeFile(board);

   return 0;
}


/**********************************************************************
* Displays the results to the screen.
***********************************************************************/
void display(char board[][9])
{
   cout << " " << board[0][0]
        << " | " << board[1][0]
        << " | " << board[2][0] << " " << endl
        << "---+---+---" << endl
        << " " << board[0][1]
        << " | " << board[1][1]
        << " | " << board[2][1] << " " << endl
        << "---+---+---" << endl
        << " " << board[0][2]
        << " | " << board[1][2]
        << " | " << board[2][2] << " " << endl;
}

void readFile(char board[][9])
{

   char sourceFile[256];


   ifstream fin;


   cout << "Enter source filename: ";
   cin >> sourceFile;

   fin.open(sourceFile);
   if (fin.fail())
   {
      cout << "Input file opening failed.\n";
      exit(1);
   }


    for (int i = 0; i < 3; i++)
    {
    for (int j = 0; j < 3; j++) {
        fin >> board[j][i];
		if (board[j][i] == '.')
        {
			board[j][i] = ' ';}
		else{
			board[j][i];

     }
    }
    }



   //Close the file
   fin.close();
}


/**********************************************************************
* Write a file into memory.
***********************************************************************/
void writeFile(char board[][9])
{

   ofstream fout;
   char destinationFile[256];
   cout << "Enter destination filename: ";
   cin >> destinationFile;


   fout.open(destinationFile);
   if (fout.fail())
   {
      cout << "Output file opening failed.\n";
      exit(1);
   }
   else
      cout << "File written" << endl;

   for (int i = 0; i < 3; i++)
   {
     for (int j = 0; j < 3; j++) {
          fout << board[j][i] << " ";
     }
     fout << endl;

   }
   fout.close();
}
If you say it works fine the first time you run the program, then this should do the trick

 
fout.open(destinationFile, std::ios::out | std::ios::trunc);
Last edited on
Thank you Golden for your answer,
I was trying with that code ,but I see the same results,I feel that I need to change something from
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fout.open(destinationFile);
   if (fout.fail())
   {
      cout << "Output file opening failed.\n";
      exit(1);
   }
   else
      cout << "File written" << endl;

   for (int i = 0; i < 3; i++)
   {
     for (int j = 0; j < 3; j++) {
          fout << board[j][i] << " ";
     }
     fout << endl;

....but exactly I don't have idea.
thank you for your support.
 
fout << board[j][i] << " ";


shouldn't this be board[i][j]?
I can see this message :

Notice that we are saving the file to a temporary location and then
reading it again. This means that if the below test fails, it is because
the file writing code if your program has a bug in it. Basically, the
written file should look the same as the original file.

that machine don't have heart!!!
I 'm working for this assignment 6 hours in this day.
Thank you for your idea,I have tried but that is the same results,I can pass the other test but just this test ,I have the big trouble.
Topic archived. No new replies allowed.