You can use the following to use the <string>
STL library with the Windows W API:
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
using std::endl;
using std::cout;
using std::cerr;
using tstring = std::basic_string<TCHAR>;
const std::wstring dr = L"drum-";
// OR: const tstring dr = TEXT("drum-");
const std::wstring no = L"normal-";
const std::wstring so = L"soft-";
const std::wstring src = L"drum.wav";
const std::wstring dest = L"hitclap.wav";
int main()
{
const std::vector<wchar_t>::size_type cd_buf_len = GetCurrentDirectoryW( 0, NULL );
assert( cd_buf_len > 0 );
std::vector<wchar_t> cwd( cd_buf_len, 0 );
const DWORD cd_result = GetCurrentDirectoryW( cd_buf_len, cwd.data() );
assert(cd_result);
if (CopyFileW( (cwd.data() + src).c_str(), (cwd.data() + dr + dest).c_str(), FALSE )) {
cout << "Done." << endl;
} else {
cerr << "Error." << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
This version is fixed to correct a bug in the initial post: modifying a .c_str()
causes undefined behavior. It’s conceptually simple and uses addition as requested, but is not as efficient as it would be to allocate buffers and copy the source strings to them, or to use +=
. You might therefore really want to write:
constexpr wchar_t src[] = L"drum.wav";
/* … */
std::wstring src_path = cwd.data();
src_path += src;
To use the TCHAR
versions, change wchar_t
to TCHAR
, change L"foo"
to TEXT("foo")
, change std::wstring
to tstring
, change LPWSTR
to LPTSTR
, change GetCurrentDirectoryW
to GetCurrentDirectory
, etc.
You do not, generally, want to mix 8-bit and wide strings in the same program unless you explicitly need to input in one encoding and output in the other. If so, you must explicitly convert, such as by setting the current code page to 65001 (UTF-8) and using std::wstring_convert
.