# copying maze from txt file

This topic is 3131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

anyone care to let me know where i messed up at? i am trying to read in the characters from a text file that make up a maze into a 2d array so i can display it with COUT //code void Maze::createMaze(ifstream& mazeF,char **mazeArray,const int& column,const int &row) { char buffer; mazeF.seekg(0L,ios::beg); // return to beginning of file because count put it at end?? while(!(mazeF.eof())) { mazeF.get(buffer); cout << buffer; // this variable is putting out a weird character? for (int z=0;z < row;z++) { for (int i=0;i < column;i++) { mazeArray[z]; } } } } //.txt file ####################### ####S################## #### ################## #### ############### ####### ############### ####### ############### ####### E ####################### for sum reason it prints random types of characters

##### Share on other sites
Are you familiar with the std::getline function?

What you could do is read the file in line by line into a vector of strings, where each string would represent one horizontal line.

Are you familiar with vectors and strings?

That said, unfortunately I don't have a compiler here to help debug your stuff, but does the following code print the file properly?

char c;ifstream mazeF("maze.txt");while(mazeF.get(c)){cout << c;}

##### Share on other sites
not really with vectors...

edit
-------
no if u look up a little in my code i pretty much did the same thing to display my first character in the file by displaying the buffer variable :)

##### Share on other sites
Sorry about my late edit. :)

I made a small edit to the code below, rearranging where the endl is used. Not a fatal issue.

OK, with the string version, it would be something like this:

#include <vector>#include <string>// #include the other stuff you're already including...using std::string;using std::vector;using std::getline;...ifstream mazeF("maze.txt");string temp;vector<string> maze;while(getline(mazeF, temp)){ if("" != temp) // skip blank lines {   maze.push_back(temp); }}// print using indices, also check out iteratorsfor(size_t i = 0; i < maze.size(); i++){ for(size_t j = 0; j < maze.length(); j++) {   cout << maze[j]; } cout << endl;}

##### Share on other sites
thx for the help id just really like to stick with my code :P i mean it should work i think it has something to do with the seeking back to the beginning of the file...

##### Share on other sites
That's cool. Try:

mazeF.clear();
mazeF.seekg(0, ios::beg);

See if the clear call helps. Not only does this computer have no compiler, but the network connection is frickin horrible. I can't even get to MSDN. Bleh.

##### Share on other sites
WOOT it worked :) thx why was that needed?

##### Share on other sites
Not exactly sure. I'm thinking it was in some kind of bad state -- perhaps just simply end of file (which I know seems kind of lame since you rewound the file, but such is the way things are), perhaps something more complicated given that you're taking the time to test for eof. Sorry for the confusing and ambiguous answer. Wish I could tell you more. :)

If you could provide the whole code, I could look deeper.

Glad to hear it works. Please give the vector/string approach an honest look-over though. You will save yourself many headaches in the future, given that you won't need to worry about pre-determining how much memory to allocate, potential memory leaks, etc.

##### Share on other sites
If you used proper tools, you would not have had to count the length of the file, and therefore would not have had the resulting problem. You would also not have to do any of the memory management work yourself (and I can practically guarantee that you have it wrong as it stands; this is not easy stuff). You were already told in the other thread not to do the memory management work yourself. It isn't a fun thing to do.

As is, the code isn't even neatly organized: you have to prepare in one place and actually do the work somewhere else. The function should be able to take care of its own setup.

Further, in C++, we use constructors to initialize things. Although it looks like either 'Maze' is a namespace in your code (rather than a class), or else you've missed the point of classes entirely (they are not a place to stuff functions; they define a data type).

The C++ I know and love tolerate looks like this:

class Maze {  std::vector<std::string> data;  public:  Maze(const std::string& filename);  // etc.}Maze::Maze(const std::string& filename) {  std::ifstream file;  file.exceptions(ios::failbit | ios::badbit);  file.open(filename);  std::string line;  while (std::getline(file, line)) { data.push_back(line); }}

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
632570
• Total Posts
3007123

×