Sign in to follow this  
MatsVed

Saving wchar_t* to file?

Recommended Posts

MatsVed    100
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
MatsVed    100
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
Antheus    2409
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
MatsVed    100
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
XTAL256    106
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this