我在C++中使用VS2010。部分寫入文件 - C++
我有一堂課名爲Room。房間有一個x和一個y屬性,然後是一堆其他屬性。
我有一個名爲Rooms.txt的文件,它包含一堆'房間'對象的屬性。它使用了自定義的語法,這樣一個條目可能是這樣的:
$ X $ Y $名稱$大小$類型$狀態$ EOL @ $
所以,我得到了以下工作正常:在播放器輸入X和Y座標,並將文本文件中相應條目的值提供給Room對象。
然後我想要做的是允許玩家更改這些變量中的一些並將它們寫回文件,但我無法弄清楚如何。這是我迄今爲止的最大努力:
ifstream inRoom;
ofstream outRoom;
string path = saveRoot;
path.append(ModLoad); // the last two lines get the path for the file to be read/written
inRoom.open(path);
//outRoom.open(path);
string temp;
string temp2;
if(inRoom.bad()){
cout << "ERROR: Bad save!";
}
while(inRoom.good()){
temp2 = temp;
getline(inRoom,temp,'$');
cout << temp <<inRoom.tellg();
if ((atoi(temp2.c_str())==myPlayer.X)&&(atoi(temp.c_str())==myPlayer.Y)){
int seek = inRoom.tellg();
inRoom.close();
cout << "Opening " << path << "for writing \n";
outRoom.open(path);
outRoom.seekp(seek);
outRoom.write("HELLO!", 6);
outRoom.close();
}
}
inRoom.close();
此外,我需要覆蓋的值,而不是插入新的數據。有什麼想法嗎?
如果你的「區域」是從疏到XY一個seekpos公式可能會非常棘手。如果區域很稀疏,我會使用排序的跳轉表開始文件。 –