473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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.

May 6 '06 #1
12 1946

"Felix85" <a.******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.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.
May 6 '06 #2
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";
}

May 6 '06 #3
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;
}

May 6 '06 #4

"Felix85" <a.******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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.
May 6 '06 #5

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:CQ*************@fe07.lga...

"Felix85" <a.******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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.
May 6 '06 #6
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.

May 6 '06 #7
sry your last post didn't show up before i posted.

May 6 '06 #8
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.


May 6 '06 #9
that was the problem. thanks Markus. and thanks to everyone else who
posted.

May 6 '06 #10
I'm lost. I found the error, but don't know what's producing it. Marked
inline

"Felix85" <a.******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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;
}

May 6 '06 #11
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.
May 6 '06 #12
Jim Langston wrote:
I'm lost. I found the error, but don't know what's producing it. Marked
inline

"Felix85" <a.******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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;
}


May 7 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Albert Greinöcker | last post by:
Hi ng, I have a very strange problem within my c# application: One of the (background) Threads in my application is responsible for some image processing tasks (like resizing, clipping, ..) on a...
3
by: sredd01 | last post by:
Hello, Please look the code below where I am reading the first 2,2,4 bytes from a binary file using two methods. I am getting a wierd (wrong) output with ifstream and memcpy method, but get the...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
1
by: utab | last post by:
Hi there I am trying to read from a file, I am trying to read certain fields,there are 6 fields in this file like --------/--------/--------/--------/--------/--------/ All fields are 8...
2
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got...
2
by: Mohammad Omer Nasir | last post by:
Hi, I tried to write a utility for copy video file from DVD-RW Rom. When I tried to access file from DVD-RW ROM, it trows exception "Access to the path 'J:\VIDEO_TS\VIDEO_TS.BUP' is denied.". I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.