Question:
C++ question: How do I implement the following? It is supposed to make use of sprintf_s.?
vtrap2009
2008-10-10 08:17:10 UTC
I have never used sprintf_s before!? This is the assignment:

class Maze {
public:
Maze();
bool loadMaze(int level); //loads maze0.txt for level 0 or
// maze1.txt for level 1, etc. Returns true iff file was found
//(you may assume it is valid).
void display() const; //prints the maze to the screen
//for this to be more useful in GacMan you are required to print
//the maze ONE character at a time regardless of how you store it
private:
//whatever you need here, mazes will 23 wide by 21 high
};

And, this is what I've got so far in a separate .cpp file:

#include "Maze.h"
#include
#include
#include

#include
using namespace std;

Maze::Maze(){}

//loads maze0.txt for level 0 or
// maze1.txt for level 1, etc. Returns true iff file was found
//(you may assume it is valid).
bool Maze::loadMaze(int level) {
int max=23;
char fileName[24];
sprintf_s(fileName,23,"maze%d.txt",level);

//create ifstream and open file:
ifstream input(fileName);
if (input.is_open())
{
while(!input.eof())
{
input.getline(fileName,max,'\n');
}
return true;
}
return false;
input.close(); //close file
}
void Maze::display()const {}
void main(){
Maze test;
cout<<"Outputs a 1 if maze was successfully loaded: "<}

I'm having problems loading the text files. It doesn't appear to even open it??
Three answers:
Rah-Mon Heur
2008-10-10 09:14:18 UTC
you overwrite each line you read from the file with the next one...



if (input.is_open())

{

while(!input.eof())

{ input.getline(fileName,max,'\n'); } }

return true;

}



note that each time you read a line from the file into the fileName buffer (which in itself is wrong, get a different buffer for that) you overwrite it's previous content. You need to store it somewhere (in members of your maze class) before reading the next one and overwriting it with the next line content.



Edit: That mean that instead of using fileName (a local variable) to receive the content of the file, you use a buffer that is a member of (declared inside) your class. That way it's accessible from all methods of that class.
2008-10-10 16:04:15 UTC
That looks o.k try debugging the code and stepping through

the code to see what value filename is and check whether or not

input.is_open() is true. My guess is some problem with

the path maybe try providing the full path to ensure you

have the current path set correctly and have some more space

than 24 characters its not a good idea to store the path of a

file.



char filename[255];

char programRootDirectory[128]= {"c:\\program location"};

sprintf_s(fileName,sizeof(fileName),

"%s\\maze%d.txt",

programRootDirectory,

level);

but you need to step through the code and check

what the values actually are with your debugger.
?
2008-10-10 15:37:09 UTC
not sure though, not good in C++

but those are long strings of code


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...