• Advertisement
Sign in to follow this  

using a WCHAR * as a filename with ofstream

This topic is 3872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, i am having an issue where i have a file name of type WCHAR * and I can not seem to get it to work with either ofstream or wofstream. Both of them give me the following error: "cannot convert parameter 1 from 'WCHAR[128] to const char *" Here is my code:
WCHAR name1[128];

StringCchPrintf( name1, 128, L"%s.brdfmap", basefilename);
ofstream scalefile( name1 );
If anyone can help me get this to work or fix it so it does work, please let me know. Also, the reason i am not using sprintf is that it is deprecated and thus should not be used. I will go back to using it if there is no other way but im sure there is one and I just dont know it. Thanks agains for everyones help.

Share this post


Link to post
Share on other sites
Advertisement
This will do the trick for you:

#include <string>
using std::wstring;
using std::string;

#include <fstream>
using std::ofstream;


int main(void)
{
wstring ws(L"Hello Wide World!");

string s(ws.begin(), ws.end());

ofstream o(s.c_str());

// or...

ofstream o2(string(ws.begin(), ws.end()).c_str());

return 0;
}

Share this post


Link to post
Share on other sites
The C++ char type on this x86 CPU is 8-bit, which would only support 256 of the 65536 16-bit Unicode characters.

Locale support in C++ may be something to look into.

Share this post


Link to post
Share on other sites
I don't have my compiler installed, so I couldn't check the following code. It is for wchar_t (WCHAR) as the input:


#include <string>
using std::string;

#include <fstream>
using std::ofstream;


int main(void)
{
wchar_t name1 = "Hello!";
size_t name1_len = 6;

string s(name1[0], name1[name1_len]);

ofstream o(s.c_str());

// or...

ofstream o2(string(ws.begin(), ws.end()).c_str());

return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement