2014-01-23 28 views
-2

我在C++中創建基於文本的冒險遊戲,並希望能夠爲某些區域提供通用描述。根據玩家位置更新文件描述

例如,區域[600,20],[1600,20],[1600,240],[600,240]可能需要描述「寬敞的牧場」。我設法自己做了區域檢查;

struct point{ 
int x; 
int y; 
}; 

bool inarea = false; 

point TL = {600,20}; 
point BR = {1600,240}; 

if(location[0]<=TL.x || location[0]>=BR.x){ 
    inarea = false; 
    cout << "Outside area, fails on X" << endl; 
return; 
} 
if(location[1]<=TL.y || location[1]>=BR.y){ 
    inarea = false; 
    cout << "Outside area, fails on Y" << endl; 
return; 
} 
inarea = true; 
cout << "In area" << endl; 
return; 

但我不知道我可以從文件中給出了具體的格式,例如加載方面:

600,20 1600,240敞開牧場

我我已經搜索了網絡,並相信我需要看看fstream,但沒有設法找到任何解釋它很簡單/逐行的內容。 感謝您的幫助。

+0

的關鍵詞,你似乎缺少的是「解析」或「解析」。用「C++解析文本文件」給我們的好朋友Google提供幫助,你會發現你需要的東西,包括StackOverflow的大量答案。 –

+0

@kuroineko這似乎是問題!我知道這是簡單的,我不知道。乾杯 – user2483876

回答

1

您可以使用不便這樣解析

#include <stdio.h> 
int main(){ 
    char *in = "600,20 1600,240 Wide open pasturelands"; 
    int a, b, c, d; 
    char discription[100]; 
    sscanf(in, "%d,%d %d,%d %[^\n]s", &a, &b, &c, &d, discription); 
    printf(" a = %d\n b = %d\n c = %d\n d = %d\n disc = %s\n",a,b,c,d, discription); 
}