Jump to content
  • Advertisement
Sign in to follow this  
MatsVed

Saving wchar_t* to file?

This topic is 3052 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

I'm trying to save a wchar_t* to file using a DLL that is run by Unreal (UDK)...
// SaveLib.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>

using namespace std;

extern "C"
{
	__declspec(dllexport) unsigned int SaveGame(wchar_t* LevelName, wchar_t* SavePath)
	{
		static ofstream SaveStream(SavePath, ios::out);
		SaveStream << LevelName;
		SaveStream.close();

		MessageBox(0, LevelName, SavePath, MB_OKCANCEL);

		return 1;
	}
}
This produces the string '10396BE0' in the save file. Doing
SaveStream << *LevelName;
produces the string '84' in the save file. Is this the address of the pointer? I thought that dereferencing the pointer would retrieve the string stored at the address?! How do I know the string is stored?

Share this post


Link to post
Share on other sites
Advertisement
10396BE0 is the address stored in the pointer, 84 is the (first) value stored at that address.

For a quick solution, try:
wstring temp = LevelName;
SaveStream << temp;

Share this post


Link to post
Share on other sites
wofstream SaveStream(SavePath); // Why static?  ios::out is the default
SaveStream << LevelName;

Share this post


Link to post
Share on other sites
D'oh!
Thanks guys! :)

mattd: Hmm... I thought you needed to declare all variables static, but when you mentioned it, I noticed that the example only declares a struct as static to ensure the struct's memory is valid after the function returns.

Linky

Share this post


Link to post
Share on other sites
another alternative if you don't want to use wofstream:


wchar_t* str = L"text";
std::ofstream out("file.ext");
//write as memory block instead of a string
out.write((char*)str, wcslen(str) * sizeof(wchar_t));

Share this post


Link to post
Share on other sites
Quote:
I'm trying to save a wchar_t* to file using a DLL that is run by Unreal (UDK)...


For what purpose? What will this string be used for?


I submit a unit test:
const char * tempfile = "r:\\test.xyz";

void test(const std::wstring & s) {
std::wofstream fout(tempfile);
std::wstring sout(s);
fout << s;
fout.close();

std::wifstream fin(tempfile);
std::wstring readback;
fin >> readback;

std::cout << (s == readback ? "Pass\n" : "String mismatch\n");
}

int main() {
test(L"HelloWorld");
test(L"\u3000\u3001\u3002");
}

It fails if any character cannot be represented with default locale, resulting in no character being printed out.

Quote:
wchar_t* str = L"text";
std::ofstream out("file.ext");
out.write((char*)str, wcslen(str) * sizeof(wchar_t));

This is better, with the added issue that wchar_t is not standardized. In addition, this requires file to be opened and treated as binary, since it may contain zeros. Whatever is printed out may also not be readable.

Share this post


Link to post
Share on other sites
Thanks, I like the idea of unit tests!
The string will be used to store the name of a level to be loaded by Unreal. I'm not exactly how the loading will happen yet though, I'm still brushing up on UnrealScript.

Share this post


Link to post
Share on other sites
Quote:
Original post by MatsVed
I thought you needed to declare all variables static, but when you mentioned it, I noticed that the example only declares a struct as static to ensure the struct's memory is valid after the function returns.

No, what would be the point of the static keyword if all variables needed to be static [grin].
What the static keyword means in this case is that the variable is created once, the first time the function is called, and exists on the heap for the life of the program (i think). It's sort of like declaring it as global but only with function scope, if that makes sense.
So you do NOT want to be declaring your function variables as static if you only want them to last for the duration of the function call. In your code you close the stream which is opened when it is created. So the next time your function will be called, the stream will already be closed but it won't be re-opened.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!