Windows API - 使用WriteFile编写std :: string的正确方法是什么?

Ele*_*rks 1 c++ winapi

BOOL WINAPI WriteFile(
  _In_         HANDLE hFile,
  _In_         LPCVOID lpBuffer,
  _In_         DWORD nNumberOfBytesToWrite,
  _Out_opt_    LPDWORD lpNumberOfBytesWritten,
  _Inout_opt_  LPOVERLAPPED lpOverlapped
);
Run Code Online (Sandbox Code Playgroud)

WriteFile采用const void*.如何将std :: string转换为const void*,以便将字符串的内容写入磁盘?

Dav*_*son 5

您可以使用c_str()获得char *从你std::string,然后你可能只需要通过直接作为第二个参数WriteFile.如果没有,那么只需将指针强制转换为LPCVOID.

有关更多信息c_str(),请参阅: std :: string to char*

  • 你不需要施放它. (3认同)