C ++中的多个字符

时间:2015-02-21 10:02:28

标签: c++

如何正确连接DataBuffer?从PHP到C ++并不容易哈哈,我到底错在了什么?

while(true) {
    DWORD TitleID = XamGetCurrentTitleId();
    char DataBuffer[] = "Here's the current title we're on : ";
    char DataBuffer[] = (DWORD)TitleID;
    char DataBuffer[] = "\n\n";
    DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
}

2 个答案:

答案 0 :(得分:2)

在C ++中,通常使用std::string类型。要从多个输入创建字符串,特别是如果它们不是所有字符串,我们使用ostringstream

所以你可能会建立这样的信息:

while(true) {
    DWORD TitleID = XamGetCurrentTitleId();
    std::ostringstream titleMessageSS;
    titleMessageSS << "Here's the current title we're on : "
                   << TitleID // already a DWORD, no need for the cast
                   << "\n\n";
    std::string titleMessage = titleMessageSS.str(); // get the string from the stream
    DWORD dwBytesToWrite = (DWORD)titleMessage.size();
    // You don't do anything with this, so can't help you with how to write the string...
}

现在,如果您使用WriteToFile,则需要从字符串中获取char指针。使用titleMessage.c_str()

执行此操作

或者,您可以使用+来构建std::string,并使用std::to_string转换TitleID(因此您可以使用+ string {{1}})。

答案 1 :(得分:1)

虽然PHP有自动转换为字符串,但C ++没有(除了某些情况)。您(通常)必须明确您的类型。幸运的是,C ++ 11提供了std::to_string,这使得这更容易:

while(true) {
    DWORD TitleID = XamGetCurrentTitleId();
    std::string DataBuffer = "Here's the current title we're on : " 
                           + std::to_string(TitleID)
                           + "\n\n";
    DWORD dwBytesToWrite = static_cast<DWORD>(DataBuffer.size());
    /* ... */
}