1
BOOL WINAPI WriteFile(
  _In_         HANDLE hFile,
  _In_         LPCVOID lpBuffer,
  _In_         DWORD nNumberOfBytesToWrite,
  _Out_opt_    LPDWORD lpNumberOfBytesWritten,
  _Inout_opt_  LPOVERLAPPED lpOverlapped
);

WriteFile takes a const void *. How do I turn an std::string into the const void * so I can write the contents of the string to disk?

  • If you care to read that information back from a file it is a good idea to store the length in front of the contents. It makes reading the data back easier (you already know the length) and doesn't break for strings with embedded NUL characters. – IInspectable Dec 22 '13 at 18:36

2 Answers2

5

You can use c_str() to get a char * from your std::string, and then you can probably just pass that directly as the second argument to WriteFile. If not, then just cast the pointer to LPCVOID.

For more information about c_str(), see: std::string to char*

Community
  • 1
  • 1
David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

As you already know, LPCVOID is just a typedef for CONST void*. Therefore you can pass a const char* to the function. std::string provides a function for that called c_str(). No cast is needed as they are both const qualified.