file reading problem

F

Felix85

here is my method for reading in a file:

static room room::file2Room(int rnum){
ostringstream filename;
filename << "../gamefiles/rooms/" << rnum <<
".room";
ifstream infile(filename.str().c_str());
string roomNameIn, roomDescriptionIn;
int roomExitsIn[6];
infile >> rnum;
getline(infile, roomNameIn);
getline(infile, roomDescriptionIn);
infile >> roomExitsIn[0] >> roomExitsIn[1] >>
roomExitsIn[2] >> roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5];
room r;
r.setRoomNumber(rnum);
r.setRoomName(roomNameIn);
r.setRoomDescription(roomDescriptionIn);
r.setRoomExits(roomExitsIn[0], roomExitsIn[1],
roomExitsIn[2], roomExitsIn[3], roomExitsIn[4], roomExitsIn[5]);
return r;
}

there are no compile errors but it reads in the file incorrectly.

here is the file it reads from:
0
Test Room
You are standing in a test room
1 2 -1 4 5 6


when i output the result to the screen it comes up:
Test Room
[ ]


instead of:
Test Room
You are standing in a test room
[ n s w u d ]

ive been trying to fix this for about 6hrs and still cant find the
problem.
any help will be appreciated.
 
J

Jim Langston

Felix85 said:
here is my method for reading in a file:

static room room::file2Room(int rnum){
ostringstream filename;
filename << "../gamefiles/rooms/" << rnum <<
".room";
ifstream infile(filename.str().c_str());

if ( ! infile.open() )
// Do something for error here.
// Display a message, return an r with rnum -1 or something
string roomNameIn, roomDescriptionIn;
int roomExitsIn[6];
infile >> rnum;

if ( ! infile >> rnum )
// Do something for error here.
// Display a message, return an r with rnum -1 or something
getline(infile, roomNameIn);

if ( ! getline( infile, roomNameIn ) )
// Do something for error here.
// Display a message, return an r with rnum -1 or something
getline(infile, roomDescriptionIn);

if ( ! getline(infile, roomDescriptionIn) )
// Do something for error here.
// Display a message, return an r with rnum -1 or something
infile >> roomExitsIn[0] >> roomExitsIn[1] >>
roomExitsIn[2] >> roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5];

if ( ! infile >> roomExitsIn[0] >> roomExitsIn[1] >>
roomExitsIn[2] >> roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5] )
// Do something for error here.
// Display a message, return an r with rnum -1 or something
room r;
r.setRoomNumber(rnum);
r.setRoomName(roomNameIn);
r.setRoomDescription(roomDescriptionIn);
r.setRoomExits(roomExitsIn[0], roomExitsIn[1],
roomExitsIn[2], roomExitsIn[3], roomExitsIn[4], roomExitsIn[5]);
return r;
}

there are no compile errors but it reads in the file incorrectly.

here is the file it reads from:
0
Test Room
You are standing in a test room
1 2 -1 4 5 6


when i output the result to the screen it comes up:
Test Room
[ ]


instead of:
Test Room
You are standing in a test room
[ n s w u d ]

ive been trying to fix this for about 6hrs and still cant find the
problem.
any help will be appreciated.

Put in the testing code and see if you get an error anywhere.

Otherwise, not enough information. Let us see how the class r is defined.
Let us see where you are trying to display the class r.
 
F

Felix85

The room object:
room(int rnum, string rname, string rdesc, int rexits[]){
roomNumber = rnum;
roomName = rname;
roomDescription = rdesc;
int i;
for(i = 0; i < sizeof(roomExits); i++){
roomExits = rexits;
}
}

stringifying the room object:
friend ostream& operator << (ostream& os, room& r){
string rooms;
if(r.roomExits[0] >= 0){
rooms += " n";
}
if(r.roomExits[1] >= 0){
rooms += " s";
}
if(r.roomExits[2] >= 0){
rooms += " e";
}
if(r.roomExits[3] >= 0){
rooms += " w";
}
if(r.roomExits[4] >= 0){
rooms += " u";
}
if(r.roomExits[5] >= 0){
rooms += " d";
}
return os << r.roomName << "\n" <<
r.roomDescription << "\n" << "[" << rooms << " ]\n";
}
 
F

Felix85

actually here is a better idea ill just post the whole file:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

class room {
public:
room() { }
room(int rnum, string rname, string rdesc, int rexits[]){
roomNumber = rnum;
roomName = rname;
roomDescription = rdesc;
int i;
for(i = 0; i < sizeof(roomExits); i++){
roomExits = rexits;
}
}

void room::setRoomNumber(int rnum){
roomNumber = rnum;
}

void room::setRoomName(string rname){
roomName = rname;
}

void room::setRoomDescription(string rdesc){
roomDescription = rdesc;
}

void room::setRoomExits(int n, int s, int e, int w, int u, int d){
roomExits[0] = n; roomExits[1] = s; roomExits[2] = e;
roomExits[3] = w; roomExits[4] = u; roomExits[5] = d;
}

int room::getRoomNumber(){
return roomNumber;
}

string room::getRoomName(){
return roomName;
}

string room::getRoomDescription(){
return roomDescription;
}

int* room::getRoomExits(){
return roomExits;
}

void room::room2File(){
ostringstream filename;
filename << "../gamefiles/rooms/" << roomNumber << ".room";
ofstream outfile(filename.str().c_str());

outfile << roomNumber << "\n";
outfile << roomName << "\n";
outfile << roomDescription << "\n";
outfile << roomExits[0] << " ";
outfile << roomExits[1] << " ";
outfile << roomExits[2] << " ";
outfile << roomExits[3] << " ";
outfile << roomExits[4] << " ";
outfile << roomExits[5] << " ";
outfile.close();
}
static room room::file2Room(int rnum){
ostringstream filename;
filename << "../gamefiles/rooms/" << rnum << ".room";
ifstream infile(filename.str().c_str());
string roomNameIn, roomDescriptionIn;
int roomExitsIn[6];
infile >> rnum;
getline(infile, roomNameIn);
getline(infile, roomDescriptionIn);
infile >> roomExitsIn[0] >> roomExitsIn[1] >> roomExitsIn[2] >>
roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5];
room r;
r.setRoomNumber(rnum);
r.setRoomName(roomNameIn);
r.setRoomDescription(roomDescriptionIn);
r.setRoomExits(roomExitsIn[0], roomExitsIn[1], roomExitsIn[2],
roomExitsIn[3], roomExitsIn[4], roomExitsIn[5]);
return r;
}

private:
int roomNumber;
string roomName, roomDescription;
int roomExits[6];
friend ostream& operator << (ostream& os, room& r){
string rooms;
if(r.roomExits[0] >= 0){
rooms += " n";
}
if(r.roomExits[1] >= 0){
rooms += " s";
}
if(r.roomExits[2] >= 0){
rooms += " e";
}
if(r.roomExits[3] >= 0){
rooms += " w";
}
if(r.roomExits[4] >= 0){
rooms += " u";
}
if(r.roomExits[5] >= 0){
rooms += " d";
}
return os << r.roomName << "\n" << r.roomDescription << "\n" << "["
<< rooms << " ]\n";
}
};

int main(void){
room r;
r.setRoomNumber(0);
r.setRoomName("Test Room");
r.setRoomDescription("You are standing in a test room");
r.setRoomExits(1, 2, -1, 4, 5, 6);
r.room2File();
cout << r;

room r2;
r2 = room::file2Room(0);
cout << r2;
return 0;
}
 
J

Jim Langston

Felix85 said:
actually here is a better idea ill just post the whole file:
[code to program snipped]

I compiled your program and ran it after creating a 0.room file.

This is my output:
Test Room
You are standing in a test room
[ n s w u d ]


[ ]

I suspect that you do not have your room file in the right location so it's
failing on open.

Add at least the check for open I showed.
 
J

Jim Langston

Jim Langston said:
Felix85 said:
actually here is a better idea ill just post the whole file:
[code to program snipped]

I compiled your program and ran it after creating a 0.room file.

This is my output:
Test Room
You are standing in a test room
[ n s w u d ]


[ ]

I suspect that you do not have your room file in the right location so
it's failing on open.

Add at least the check for open I showed.

Ooops, igore that, because I didn't notice you were outputting r twice.

Let me do some checking.
 
F

Felix85

if you look at the room2File method it shows you the directory it puts
the 0.room file in.
.../gamefiles/rooms/0.room
and the file is in the right place.
 
M

Markus Schoder

Felix85 said:
here is my method for reading in a file:

static room room::file2Room(int rnum){
ostringstream filename;
filename << "../gamefiles/rooms/" << rnum <<
".room";
ifstream infile(filename.str().c_str());
string roomNameIn, roomDescriptionIn;
int roomExitsIn[6];
infile >> rnum;

This reads up to the newline but does not consume it.
getline(infile, roomNameIn);

This starts at the newline and therefore reads an empty string. From
then on everything is out of sync. You could insert a dummy
getline(...) before this one.
getline(infile, roomDescriptionIn);
infile >> roomExitsIn[0] >> roomExitsIn[1] >>
roomExitsIn[2] >> roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5];
room r;
r.setRoomNumber(rnum);
r.setRoomName(roomNameIn);
r.setRoomDescription(roomDescriptionIn);
r.setRoomExits(roomExitsIn[0], roomExitsIn[1],
roomExitsIn[2], roomExitsIn[3], roomExitsIn[4], roomExitsIn[5]);
return r;
}

there are no compile errors but it reads in the file incorrectly.

here is the file it reads from:
0
Test Room
You are standing in a test room
1 2 -1 4 5 6


when i output the result to the screen it comes up:
Test Room
[ ]


instead of:
Test Room
You are standing in a test room
[ n s w u d ]

ive been trying to fix this for about 6hrs and still cant find the
problem.
any help will be appreciated.
 
J

Jim Langston

I'm lost. I found the error, but don't know what's producing it. Marked
inline

Felix85 said:
actually here is a better idea ill just post the whole file:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

class room {
public:
room() { }
room(int rnum, string rname, string rdesc, int rexits[]){
roomNumber = rnum;
roomName = rname;
roomDescription = rdesc;
int i;
for(i = 0; i < sizeof(roomExits); i++){
roomExits = rexits;
}
}

void room::setRoomNumber(int rnum){
roomNumber = rnum;
}

void room::setRoomName(string rname){
roomName = rname;
}

void room::setRoomDescription(string rdesc){
roomDescription = rdesc;
}

void room::setRoomExits(int n, int s, int e, int w, int u, int d){
roomExits[0] = n; roomExits[1] = s; roomExits[2] = e;
roomExits[3] = w; roomExits[4] = u; roomExits[5] = d;
}

int room::getRoomNumber(){
return roomNumber;
}

string room::getRoomName(){
return roomName;
}

string room::getRoomDescription(){
return roomDescription;
}

int* room::getRoomExits(){
return roomExits;
}

void room::room2File(){
ostringstream filename;
filename << "../gamefiles/rooms/" << roomNumber << ".room";
ofstream outfile(filename.str().c_str());

outfile << roomNumber << "\n";
outfile << roomName << "\n";
outfile << roomDescription << "\n";
outfile << roomExits[0] << " ";
outfile << roomExits[1] << " ";
outfile << roomExits[2] << " ";
outfile << roomExits[3] << " ";
outfile << roomExits[4] << " ";
outfile << roomExits[5] << " ";
outfile.close();
}
static room room::file2Room(int rnum){
ostringstream filename;
filename << "../gamefiles/rooms/" << rnum << ".room";
ifstream infile(filename.str().c_str());
string roomNameIn, roomDescriptionIn;
int roomExitsIn[6];
infile >> rnum;


This is not happening. It's ignoring the line with the value 0 but not
throwing any errors or moving along the file pointer. I've tried creating a
different var to read into such as:
int rN;
infile >> rN;

but it's still not reading.

If you ignore the room number and just discard the line

std::string temp;
getline( infile, rN );

everything else works fine.

I don't know why this is. The file appears to be perfectly fine.

0
Test Room
You are standing in a test room
1 2 -1 4 5 6

This has me completely stumped. I tried writing a 1 instead of a 0 and it
wouldn't read that either.

I'm confused. Hopefully someone else will see this post and have an idea.
getline(infile, roomNameIn);
getline(infile, roomDescriptionIn);
infile >> roomExitsIn[0] >> roomExitsIn[1] >> roomExitsIn[2] >>
roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5];
room r;
r.setRoomNumber(rnum);
r.setRoomName(roomNameIn);
r.setRoomDescription(roomDescriptionIn);
r.setRoomExits(roomExitsIn[0], roomExitsIn[1], roomExitsIn[2],
roomExitsIn[3], roomExitsIn[4], roomExitsIn[5]);
return r;
}

private:
int roomNumber;
string roomName, roomDescription;
int roomExits[6];
friend ostream& operator << (ostream& os, room& r){
string rooms;
if(r.roomExits[0] >= 0){
rooms += " n";
}
if(r.roomExits[1] >= 0){
rooms += " s";
}
if(r.roomExits[2] >= 0){
rooms += " e";
}
if(r.roomExits[3] >= 0){
rooms += " w";
}
if(r.roomExits[4] >= 0){
rooms += " u";
}
if(r.roomExits[5] >= 0){
rooms += " d";
}
return os << r.roomName << "\n" << r.roomDescription << "\n" << "["
<< rooms << " ]\n";
}
};

int main(void){
room r;
r.setRoomNumber(0);
r.setRoomName("Test Room");
r.setRoomDescription("You are standing in a test room");
r.setRoomExits(1, 2, -1, 4, 5, 6);
r.room2File();
cout << r;

room r2;
r2 = room::file2Room(0);
cout << r2;
return 0;
}
 
L

Larry I Smith

Jim said:
I'm lost. I found the error, but don't know what's producing it. Marked
inline

Felix85 said:
actually here is a better idea ill just post the whole file:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

class room {
public:
room() { }
room(int rnum, string rname, string rdesc, int rexits[]){
roomNumber = rnum;
roomName = rname;
roomDescription = rdesc;
int i;
for(i = 0; i < sizeof(roomExits); i++){
roomExits = rexits;
}
}

void room::setRoomNumber(int rnum){
roomNumber = rnum;
}

void room::setRoomName(string rname){
roomName = rname;
}

void room::setRoomDescription(string rdesc){
roomDescription = rdesc;
}

void room::setRoomExits(int n, int s, int e, int w, int u, int d){
roomExits[0] = n; roomExits[1] = s; roomExits[2] = e;
roomExits[3] = w; roomExits[4] = u; roomExits[5] = d;
}

int room::getRoomNumber(){
return roomNumber;
}

string room::getRoomName(){
return roomName;
}

string room::getRoomDescription(){
return roomDescription;
}

int* room::getRoomExits(){
return roomExits;
}

void room::room2File(){
ostringstream filename;
filename << "../gamefiles/rooms/" << roomNumber << ".room";
ofstream outfile(filename.str().c_str());

outfile << roomNumber << "\n";
outfile << roomName << "\n";
outfile << roomDescription << "\n";
outfile << roomExits[0] << " ";
outfile << roomExits[1] << " ";
outfile << roomExits[2] << " ";
outfile << roomExits[3] << " ";
outfile << roomExits[4] << " ";
outfile << roomExits[5] << " ";
outfile.close();
}
static room room::file2Room(int rnum){
ostringstream filename;
filename << "../gamefiles/rooms/" << rnum << ".room";
ifstream infile(filename.str().c_str());
string roomNameIn, roomDescriptionIn;
int roomExitsIn[6];
infile >> rnum;


This is not happening. It's ignoring the line with the value 0 but not
throwing any errors or moving along the file pointer. I've tried creating a
different var to read into such as:
int rN;
infile >> rN;



You are reading a number with the above statement.
This will leave the file pointer at the newline following
the number (following the zero), i.e. the newline at
the end of line one.

Next you call getline(); it reads the newline at the end
of line one, and returns an empty string. From this point
on, all of the input is not as you expect.

Use getline() to read every line (including the first),
then parse each line for the desired fields.

but it's still not reading.

If you ignore the room number and just discard the line

std::string temp;
getline( infile, rN );

everything else works fine.

I don't know why this is. The file appears to be perfectly fine.

0
Test Room
You are standing in a test room
1 2 -1 4 5 6

This has me completely stumped. I tried writing a 1 instead of a 0 and it
wouldn't read that either.

I'm confused. Hopefully someone else will see this post and have an idea.
getline(infile, roomNameIn);
getline(infile, roomDescriptionIn);
infile >> roomExitsIn[0] >> roomExitsIn[1] >> roomExitsIn[2] >>
roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5];
room r;
r.setRoomNumber(rnum);
r.setRoomName(roomNameIn);
r.setRoomDescription(roomDescriptionIn);
r.setRoomExits(roomExitsIn[0], roomExitsIn[1], roomExitsIn[2],
roomExitsIn[3], roomExitsIn[4], roomExitsIn[5]);
return r;
}

private:
int roomNumber;
string roomName, roomDescription;
int roomExits[6];
friend ostream& operator << (ostream& os, room& r){
string rooms;
if(r.roomExits[0] >= 0){
rooms += " n";
}
if(r.roomExits[1] >= 0){
rooms += " s";
}
if(r.roomExits[2] >= 0){
rooms += " e";
}
if(r.roomExits[3] >= 0){
rooms += " w";
}
if(r.roomExits[4] >= 0){
rooms += " u";
}
if(r.roomExits[5] >= 0){
rooms += " d";
}
return os << r.roomName << "\n" << r.roomDescription << "\n" << "["
<< rooms << " ]\n";
}
};

int main(void){
room r;
r.setRoomNumber(0);
r.setRoomName("Test Room");
r.setRoomDescription("You are standing in a test room");
r.setRoomExits(1, 2, -1, 4, 5, 6);
r.room2File();
cout << r;

room r2;
r2 = room::file2Room(0);
cout << r2;
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top