2015-06-08 57 views
0

我想拆分TCHAR消息等给定的例子:分裂TCHAR成二维矢量

TCHAR [1000] = "[X][X] [X][X][X] [X][P][-]..." 

成二维矢量,看起来像这样:

enter image description here

void Comunnication::receiveMessage(tstring msg){ 

    TCHAR gameMessage[1000]; 
    vector<vector<tstring>> gameMap; 

    BOOL isSucceed = ReadFile(serverUpdatePipe, gameMessage, sizeof(gameMessage), &bytesRead, NULL); 

    gameMessage[bytesRead/sizeof(TCHAR)] = '\0'; 

    if (!isSucceed || !bytesRead){ 
     break; 
    } 

    //Wrong 
    for (DWORD i = 0; i < 15; i++) 
    { 
     vector<tstring> line; 
     for (DWORD j = 0; j < 15; j++) 
     { 
      // get 3 charaters of each time 
     } 
     communication.getMap().push_back(line); 
    } 

} 

问题这里是我不知道如何获得3个字符(地图的每个块)并将其保存在二维矢量上。

+0

你可以用'getchar'它得到每次一个字符。只需调用它三次。 – meneldal

回答

1

您可以使用这些下面的函数之一:

  • std::istream::get

您可以在here阅读有关此功能,istream& get (char* s, streamsize n);

例子:

char *s; 
cin.get(s, 4); 
cout << s << endl; 

输出:

string test 
str 
  • std::istream::read

或者您可以使用此功能,istream& read (char* s, streamsize n);。描述在here。 如果使用此功能,则必须先定义行的大小为3。

例子:

char s[3]; 
cin.read(s, 3); 
cout << s << endl; 

输出:

string test 
str