473,657 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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, roomDescription In;
int roomExitsIn[6];
infile >> rnum;
getline(infile, roomNameIn);
getline(infile, roomDescription In);
infile >> roomExitsIn[0] >> roomExitsIn[1] >>
roomExitsIn[2] >> roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5];
room r;
r.setRoomNumber (rnum);
r.setRoomName(r oomNameIn);
r.setRoomDescri ption(roomDescr iptionIn);
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 1965

"Felix85" <a.******@gmail .com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.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, roomDescription In;
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, roomDescription In);
if ( ! getline(infile, roomDescription In) )
// 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(r oomNameIn);
r.setRoomDescri ption(roomDescr iptionIn);
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(roomExit s); 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.roomDescripti on << "\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(roomExit s); i++){
roomExits[i] = rexits[i];
}
}

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

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

void room::setRoomDe scription(strin g rdesc){
roomDescription = rdesc;
}

void room::setRoomEx its(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::getRoomNu mber(){
return roomNumber;
}

string room::getRoomNa me(){
return roomName;
}

string room::getRoomDe scription(){
return roomDescription ;
}

int* room::getRoomEx its(){
return roomExits;
}

void room::room2File (){
ostringstream filename;
filename << "../gamefiles/rooms/" << roomNumber << ".room";
ofstream outfile(filenam e.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, roomDescription In;
int roomExitsIn[6];
infile >> rnum;
getline(infile, roomNameIn);
getline(infile, roomDescription In);
infile >> roomExitsIn[0] >> roomExitsIn[1] >> roomExitsIn[2] >>
roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5];
room r;
r.setRoomNumber (rnum);
r.setRoomName(r oomNameIn);
r.setRoomDescri ption(roomDescr iptionIn);
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.roomDescripti on << "\n" << "["
<< rooms << " ]\n";
}
};

int main(void){
room r;
r.setRoomNumber (0);
r.setRoomName(" Test Room");
r.setRoomDescri ption("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.goo glegroups.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*******@rock etmail.com> wrote in message
news:CQ******** *****@fe07.lga. ..

"Felix85" <a.******@gmail .com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.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, roomDescription In;
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, roomDescription In);
infile >> roomExitsIn[0] >> roomExitsIn[1] >>
roomExitsIn[2] >> roomExitsIn[3] >> roomExitsIn[4] >> roomExitsIn[5];
room r;
r.setRoomNumber (rnum);
r.setRoomName(r oomNameIn);
r.setRoomDescri ption(roomDescr iptionIn);
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

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

Similar topics

4
13630
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 list of files. Therefore, some temporary files are created which should be deleted after processing is finished. This Thread is called several times. When I call it the first time, everything's ok and no errors occur. In the second call, I can...
3
4091
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 correct output with CFile read. I am trying to get the values 1400 1050 and 2014 from the binary file. Can anybody point out the mistake in the following code?
2
2477
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() { OperatingSystem os = Environment.OSVersion; AppDomain ad = Thread.GetDomain();
1
3034
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 characters width and I am comparing the first character of the line with the $ sign which seperates different blocks of information $ Nodes of the Entire Model
2
1836
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 deleted. The content of the external file , which is in .cvs format is: -------------------------------------------------------------------
2
2429
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 tried below code for file copy utility. private void butCopyFile_Click(object sender, EventArgs e) { try
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8502
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7316
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.