views:

330

answers:

4

I'm writing code to read in a 7x15 block of text in a file that will represent a 'maze'.

#include <iostream>
#include <fstream>
#include <string>
#include "board.h"  

int main()
{
    char charBoard[7][15]; //the array we will use to scan the maze and modify it
    ifstream loadMaze("maze"); //the fstream we will use to take in a maze
    char temp; //our temperary holder of each char we read in

    for(int i = 0;i < 7; i++)
    {

     for(int j = 0; j < 15; j++)
 {
  temp= loadMaze.get();
  charBoard[i][j] = temp;
  cout << charBoard[i][j];  //testing
 }
 cout << endl;
}

return 0;
}

this was my original draft but this didnt work as it kept returning ? for each char it read. This is the maze im testing with:

  #############
              #
############  #
              #
 ######### ####
 # !       #   
############   

EDIT: The cout is printing this:

  #############


#
############ 
 #

  #
 ######### 
####
 # !      
 #   
#########

Am I not escaping \n's?

I've been coding for a few hours so I think its a simple mistake I'm not catching that's tripping me up right now. Thanks!

+1  A: 

Try an absolute path like "c:\MyMazes\maze".

Throw in a system("cd") to see where the current directory is. If you're having trouble finding the current directory, check out this SO discussion

Here's the complete code - this should display your entire maze (if possible) and the current directory.

 char charBoard[7][15];      //the array we will use to scan the maze and modify it
 system("cd");
     ifstream loadMaze("c:\\MyMazes\\maze");  //the fstream we will use to take in a maze

 if(!loadMaze.fail())
 {
 for(int i = 0;i < 7; i++)
 {
        // Display a new line
  cout<<endl;
  for(int j = 0; j < 15; j++)
  {
             //Read the maze character
       loadMaze.get(charBoard[i][j]);
       cout << charBoard[i][j];  //testing
  }
        // Read the newline
  loadMaze.get();
 }
 return 0;
 }
 return 1;
Jacob
A: 

Can you check whether extraction from file is proper: using good() API of ifstream

for(int j = 0; j < 15; j++)
{
    if(!loadMaze.good())
    {
     cout << "path incorrect";

    }

    temp= loadMaze.get();


    cout << "temp = " << temp << endl; //testing
    charBoard[i][j] = temp;
    cout << charBoard[i][j];  //testing
}

OR

in the beginning itself:

ifstream loadMaze("maze"); 
if(!loadMaze.good())
{
  //ERROR
}
aJ
Thanks! I'm indeed getting !good()...but why? Maze is in the same folder as all the files
danny z
Throw in a system("cd") to see where the current directory is
Jacob
That's actually not doing anything for me, can you eleborate on system("cd"); ?
danny z
What compiler are you using? That's supposed to print your current working directory.
Jacob
You can try by giving the absolute path : something like "c:\maze"
aJ
Looks like current working directory is different. try system("cd"); as suggested by @Jacob
aJ
ok it didn't work for textmate but in xCode it shows me the path
danny z
Are you using VS? Try to put the "maze" into the directoy that contains .vcproj .
pierr
Anybody have any other ideas?
danny z
I thought you found the path?
Jacob
ah ok, when I run it in Xcode it hides the file about 7 folders deep and I've found it. But the problem is that now it prints a mess. I'll edit my question
danny z
Did you try the code in my answer? It worked for me.
Jacob
danny z
A: 

try adding the line

if (!loadMaze) throw 1;

after the declaration of loadMaze, this will throw an exception if the file isn't there. This is a hack, really you should throw a real error. But it works to test.

rlbond
A: 

Check whether the file opening failed or not. You can find this out by checking if it's good:

http://www.cplusplus.com/reference/iostream/ios/good/

If the file opening has failed, then try writing in the absolute path to the file (C:/Documents and Settings/.../maze), to see whether that works. If it does, it's just the file path that's wrong, and you'll have to play with that.

Smashery