タイプ のバッファchar*
とstring
. string
バッファー内に長さ +を配置したいstring
.
これを達成するために次のコードを書きましたがstd::cout<<strlen(buffer)
、関数のパラメーターとして渡した文字列に関係なく「1」が出力されるため、機能しません。
int VariableLengthRecord :: pack (const std::string strToPack)
{
int strToPackSize = strToPack.length();
if (sizeof(strToPackSize) + strToPackSize > maxBytes - nextByte)
return RES_RECORD_TOO_LONG; // The string is too long
int start = nextByte;
// Copy the string length into the buffer
copyIntToBuffer((buffer+start),strToPackSize);
// Copy the string into the buffer
strcpy((buffer+start+sizeof(strToPackSize)),strToPack.c_str());
// Move the buffer pointer
nextByte += sizeof(strToPackSize) + strToPackSize;
// Update buffer size
bufferSize = nextByte;
std::cout << "Size of buffer = " << strlen(buffer) << std::endl;
return RES_OK;
}
void copyIntToBuffer (char* buffer, int integer)
{
buffer[0] = integer & 0xff;
buffer[1] = (integer >> 8) & 0xff;
buffer[2] = (integer >> 16) & 0xff;
buffer[3] = (integer >> 24) & 0xff;
}