| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Felix85" |
| Date: |
06 May 2006 04:49:03 PM |
| Object: |
file reading problem |
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.
.
|
|
| User: "Jim Langston" |
|
| Title: Re: file reading problem |
06 May 2006 05:10:54 PM |
|
|
"Felix85" <a.pyro85@gmail.com> wrote in message
news:1146952143.449082.112240@i39g2000cwa.googlegroups.com...
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.
.
|
|
|
| User: "Felix85" |
|
| Title: Re: file reading problem |
06 May 2006 05:16:11 PM |
|
|
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[i] = rexits[i];
}
}
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";
}
.
|
|
|
| User: "Felix85" |
|
| Title: Re: file reading problem |
06 May 2006 05:24:04 PM |
|
|
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[i] = rexits[i];
}
}
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;
}
.
|
|
|
| User: "Jim Langston" |
|
| Title: Re: file reading problem |
06 May 2006 05:56:29 PM |
|
|
"Felix85" <a.pyro85@gmail.com> wrote in message
news:1146954244.008513.245490@g10g2000cwb.googlegroups.com...
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.
.
|
|
|
| User: "Jim Langston" |
|
| Title: Re: file reading problem |
06 May 2006 06:00:31 PM |
|
|
"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:CQ97g.57$9A7.26@fe07.lga...
"Felix85" <a.pyro85@gmail.com> wrote in message
news:1146954244.008513.245490@g10g2000cwb.googlegroups.com...
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.
.
|
|
|
|
| User: "Felix85" |
|
| Title: Re: file reading problem |
06 May 2006 06:02:41 PM |
|
|
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.
.
|
|
|
| User: "Felix85" |
|
| Title: Re: file reading problem |
06 May 2006 06:04:30 PM |
|
|
sry your last post didn't show up before i posted.
.
|
|
|
| User: "Default User" |
|
| Title: Re: file reading problem |
06 May 2006 06:33:29 PM |
|
|
Felix85 wrote:
sry your last post didn't show up before i posted.
Who are you talking to, and about what? See below.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
.
|
|
|
|
|
|
|
| User: "Jim Langston" |
|
| Title: Re: file reading problem |
06 May 2006 06:33:00 PM |
|
|
I'm lost. I found the error, but don't know what's producing it. Marked
inline
"Felix85" <a.pyro85@gmail.com> wrote in message
news:1146954244.008513.245490@g10g2000cwb.googlegroups.com...
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[i] = rexits[i];
}
}
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;
}
.
|
|
|
| User: "Larry I Smith" |
|
| Title: Re: file reading problem |
06 May 2006 10:28:11 PM |
|
|
Jim Langston wrote:
I'm lost. I found the error, but don't know what's producing it. Marked
inline
"Felix85" <a.pyro85@gmail.com> wrote in message
news:1146954244.008513.245490@g10g2000cwb.googlegroups.com...
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[i] = rexits[i];
}
}
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;
}
.
|
|
|
|
|
|
|
|
| User: "Markus Schoder" |
|
| Title: Re: file reading problem |
06 May 2006 06:15:58 PM |
|
|
Felix85 wrote:
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.
.
|
|
|
| User: "Felix85" |
|
| Title: Re: file reading problem |
06 May 2006 06:32:51 PM |
|
|
that was the problem. thanks Markus. and thanks to everyone else who
posted.
.
|
|
|
|
|

|
Related Articles |
|
|