gpt4 book ai didi

c++ - 从 strtok 函数保存值

转载 作者:行者123 更新时间:2023-11-28 07:56:28 26 4
gpt4 key购买 nike

您好,我正在使用 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,但我还需要获取第二个值。

最佳答案

为什么不将它存储在 vector 中?

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

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

关于c++ - 从 strtok 函数保存值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12598329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com