文件阅读问题

扫码查看
本文介绍了文件阅读问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在文件中阅读的方法:


静态房间:: file2Room(int rnum){

ostringstream filename;

filename<< " ../ gamefiles /房间/" << 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];

房间r;

r.setRoomNumber(rnum);

r.setRoomName(roomNameIn);

r.setRoomDescription(roomDescriptionIn);

r.setRoomExits(roomExitsIn [0],roomExitsIn [1],

roomExitsIn [2],roomExitsIn [3] ,roomExitsIn [4],roomExitsIn [5]);

返回r;

}


没有编译错误但是它读取的文件不正确。


这里是它所读取的文件:

0

测试室

你站在一个测试室

1 2 -1 4 5 6

当我将结果输出到屏幕时出现:

测试室

[]

而不是:

测试室

你站在一个测试室

[nswud]


我一直试图解决这个问题大约6小时仍然无法找到

问题。

任何帮助将不胜感激。

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.

推荐答案




输入测试代码,看看你是否在任何地方都出现错误。


否则,信息不足。让我们看看如何定义类r。

让我们看看你在哪里展示类r。



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.




这篇关于文件阅读问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:07
查看更多