2012-07-16 87 views
0

这是link我有一个包含具有格式的数据的大名单:如何搜索并从原始网页中获取文本? VC++?

“X = MapNumber:Y = MAPNAME”

例如

“10000:蘑菇园”。

在vC++ .net中,我想编写一个连接到该链接的函数,在数据中搜索一个数字(比方说10000),然后在数字旁边获得名称(这将是蘑菇公园),然后把名字放入一个字符串中。

下面的代码是我已经和将工作,如果所有的数据都在.txt文件中,但我想将此代码转换为连接到上面的链接,并最终使其更有效。

谢谢。

String^ GrabMapNameById(String^ CurrentStr)//Current Map String{ 
if(CurrentMapIDRead.ToString() != CurrentMapID.ToString()) //If map has chnaged 
{ 
     try //Try streaming text 
     { 
      String^ Maps = "Strife.Maps";//map File Name 
      StreamReader^ MapDin = File::OpenText("Strife.Maps");//Stream Declar 

      String^ str; 
      string s; 
      int count = 0; 
      while ((str = MapDin->ReadLine()) != nullptr) //Loop for every line 
      { 
      if(str->Contains(CurrentMapID.ToString())) //Untill Map id found 
      { 
       CurrentMapIDRead = CurrentMapID; //Set Current Map Name 
       CurrentStr = str->Replace(CurrentMapIDRead.ToString() , "");//Replace map id with null characters 
       CurrentStr = CurrentStr->Replace(" =" , ""); //Take out = characters 
       break;// kill while 
      } 
      } 
     } 

      catch (Exception^ e) 
      { 
      } 
}return CurrentStr;} 
+0

如果可能,请查找[libcurl](http://curl.haxx.se/libcurl/)。 Windows二进制文件可用。 – 2012-07-16 05:06:49

回答

1

我以前没有用过vC++,但我相信你正在寻找WebRequest类来获取数据。微软在提出这样的请求时有一个tutorial。它使用流读取器,因此try块内的代码看起来像这样:

String *sURL = S"http://pastebin.com/raw.php?i=yVyxkWFD"; 
WebRequest *wrGETURL; 
wrGETURL = WebRequest::Create(sURL); 
WebProxy *myProxy = new WebProxy(S"myproxy", 80); 
myProxy->BypassProxyOnLocal = true; 

wrGETURL->Proxy = WebProxy::GetDefaultProxy(); 

Stream *objStream = wrGETURL->GetResponse()->GetResponseStream(); 

StreamReader *MapDin = new StreamReader(objStream); 

String^ str; 
string s; 
int count = 0; 
while ((str = MapDin->ReadLine()) != nullptr) //Loop for every line 
{ 
    if(str->Contains(CurrentMapID.ToString())) //Untill Map id found 
    { 
     CurrentMapIDRead = CurrentMapID; //Set Current Map Name 
     CurrentStr = str->Replace(CurrentMapIDRead.ToString() , "");//Replace map id with null characters 
     CurrentStr = CurrentStr->Replace(" =" , ""); //Take out = characters 
     break;// kill while 
    } 
}