Opening File Help

Hello.
I am attempting to open up a file and display it to the screen as a tic tac toe board.
The file would have this on it this
1
2
3
4
 
X . O
X . O
. X .


and it would be displayed as
1
2
3
 X |   | O 
 X |   | O 
---+---+---
   | X |   


This is what i have, just opening the file and such:
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
int readFile(fileName[], ttt[])
{
   ifstream fin(fileName);
   if (fin.fail())
      return 0;
   for(int i = 0; fin.eof(); i++)
      ttt[i] = fin.get();
   fin.close();
   return 1;
}


int main()
{

   char fileName[256];
   char ttt[9];
   cout << "Enter source filename: ";
   cin >> fileName[256];
   int success = readFile (fileName, ttt);
   if (success == 0)

   {
      cout << "Unable to open filename " << fileName << endl;
      return 0;
   }


so far, this has not given me issues, but i have NO idea how to display that board.
can anyone give me some help?
Last edited on
The tic-tac-toe board is a rectangular 3x3 grid. You can use a double nested for loop to iterate through each "box" and display its contents using std::cout. but because you saved it as a one dimensional array, its easier to keep track of where you are by using a counter. you could do math, but this is faster.
1
2
3
4
5
6
7
8
9
10
11
12
13

unsigned int index = 0;

for(int x = 0; x < 3; x++)   //the columns,
{
     for(int y = 0; y < 3; y++)   //the rows
     {
          std::cout<<ttt[index];
          std::cout<<ttt[++index];
          std::cout<<ttt[++index];
          std::cout<<std::endl;
     }
}


this only displays the contents of the array. If you want grid separators (pipe symbols or what have you) you'll need to add those in.

Last edited on
so i used what you said.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 int index = 0;

   for(int x = 0; x < 3; x++)

   {
      for(int y = 0; y < 3; y++)
      {
         

         cout << ttt[index] << " | "
              << ttt[++index] << " | "
              << ttt[++index] << endl;
         cout << "---+---+---" << endl;

      }
   }

and now im trying to make the periods equal spaces. where would i put
1
2
if ( ttt[index] == '.')
   cout << "   "

in the program?

UPDATE:
so i used
1
2
if ( ttt[index] == '.')
   cout << "   "

and instead of replacing the period, it just adds an exra space. how would i get rid of the period completely?
Last edited on
Instead of using two spaces and quotation marks, use a single space and an apostrophe.

I'm not sure why you want to do it like this, why not just store the values the way you would output them?

Then you would just need to loop through and output the array.
Ill be honest, I'm not too good with files and such. How exactly would I do that?
Last edited on
instead of a period you store a space.

Don't over think this.
Last edited on
Get yourself a piece of graph paper and a pencil.

Pretend each square cell is a character, and draw what you want to see on the screen.

Now, when you write to screen, go from left to right and print every square -- including spaces -- and then a newline. Repeat for each additional line. (You will print at least 5 lines.)

Drawing stuff helps a whole lot. Good luck!
Topic archived. No new replies allowed.