从strtok函数保存值

时间:2012-09-26 09:14:06

标签: c++ strtok

您好我正在使用strtok功能将一个句子分成两部分。我似乎无法弄清楚如何保存两个单独的值,该功能成功地分裂我只需要知道的分钟来存储值。感谢

extern "C" UINT __stdcall GetProductName(MSIHANDLE hInstall)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
char szProductName[MAX_PATH];
TCHAR* szValueBuf = NULL;
DWORD cchValueBuf = 0;
char * pch;
std::vector<std::string> parts;


hr = WcaInitialize(hInstall, "GetProductName");
ExitOnFailure(hr, "Failed to initialize");

WcaLog(LOGMSG_STANDARD, "Initialized.");

UINT uiStat =  MsiGetProperty(hInstall, TEXT("ProductName"), TEXT(""), &cchValueBuf);

if (ERROR_MORE_DATA == uiStat)
{
    ++cchValueBuf; 
    szValueBuf = new TCHAR[cchValueBuf];
    if (szValueBuf)
    {
        uiStat = MsiGetProperty(hInstall, TEXT("ProductName"), szValueBuf, &cchValueBuf);

    }
}
if (ERROR_SUCCESS != uiStat)
{
    if (szValueBuf != NULL) 
        delete[] szValueBuf;
    return ERROR_INSTALL_FAILURE;
}

if (Orc_Create_Product_Key(szValueBuf))
    hr = S_OK;
else
    hr = ERROR_INSTALL_FAILURE;

pch = strtok (szValueBuf," ");
parts.push_back(pch);

  while (pch != NULL)
  {
       pch = strtok (NULL, " ");
       parts.push_back(pch);
  }


strcpy(szProductName, szValueBuf);

hr = MsiSetProperty(hInstall, "PRODUCTNAME",  szProductName);
ExitOnFailure(hr, "failed to set PRODUCTNAME");

LExit:
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
 }

正如您所看到的,第一个值将是szProductName,但我还需要获得第二个值。

1 个答案:

答案 0 :(得分:1)

为什么不将它存储在矢量中?

std::vector<std::string> parts;
pch = strtok (szValueBuf," ");
parts.push_back(pch);

while (pch != NULL)
{
   pch = strtok (NULL, " ");
   parts.push_back(pch);
}