wstring到wchar_t的转换
wstring to wchar_t conversion
我使用Namedpipes通信(C++)在两个进程之间传输数据。为了方便起见,我使用wstring来传输数据,并且在传输端一切都很好。我无法在接收端接收全部数据。以下是传输结束代码。
wstringstream send_data;
send_data << "10" << " " << "20" << " " << "30" << " " << "40" << " " << "50" << " " << "60" << " " ;
DWORD numBytesWritten = 0;
result = WriteFile(
pipe, // handle to our outbound pipe
send_data.str().c_str(), // data to send
send_data.str().size(), // length of data to send (bytes)
&numBytesWritten, // will store actual amount of data sent
NULL // not using overlapped IO
);
以下是接收端代码。
wchar_t buffer[128];
DWORD numBytesRead = 0;
BOOL result = ReadFile(
pipe,
buffer, // the data from the pipe will be put here
127 * sizeof(wchar_t), // number of bytes allocated
&numBytesRead, // this will store number of bytes actually read
NULL // not using overlapped IO
);
if (result) {
buffer[numBytesRead / sizeof(wchar_t)] = ' '; // null terminate the string
wcout << "Number of bytes read: " << numBytesRead << endl;
wcout << "Message: " << buffer << endl;
}
缓冲区中的结果仅包含10 20 30有人能解释一下数据被截断的原因吗。
您没有使用WriteFile()
函数发送所有数据。您发送的send_data.str().size()
字节数不正确,因为size()
提供的是字符数,而不是字节数。您可以更改代码以使用:
send_data.str().size() * sizeof(wchar_t) / sizeof(char)
它将发送正确数量的字节。
相关文章:
- 无法将参数 1 从 WCHAR 转换为常量字符 *
- 如何将CString转换为使用WCHAR的Windows API和自定义函数?
- VC++ 2017 如何将常量wchar_t转换为 WCHAR *.
- 将WCHAR转换为LPCTSTR
- 在C++中读取和写入Windows注册表(如何将字符串转换为wchar(?))
- 在 unicode 中将 WCHAR 数组转换为 std::string?
- 将const wchar_t*转换为wchar*
- 将字符从 fget 转换为 Wchar*
- 如何在 iOS 中将 UTF-8 字符串转换为 wchar
- 如何将const char*转换为const WCHAR*
- 如何将wchar_t(或wchar_t*或CORBA::WChar*)转换为字符串
- 如何将strncpy()转换为WCHAR
- 通过COM将C#字符串转换为C++WCHAR*
- 将wchar转换为字符串并推入矢量(C/C++)
- 将 WCHAR[260] 转换为 std::string
- 无法将char*转换为WCHAR*[qt/c++]
- 在WCHAR*和wstring之间转换,而不使用构造函数
- 将std::string转换为GDI+参数所需的const WCHAR*字符串
- 如何在C++中将WCHAR*转换为字符串,反之亦然
- "strcpy":无法将参数 2 从"WCHAR *"转换为"常量字符 *