[Gdal-dev] Wide-character filenames with GDAL file IO?

Ben Discoe ben at vterrain.org
Tue Sep 19 17:47:22 EDT 2006


> -----
> From: Mateusz Loskot
> Sent: Tuesday, September 19, 2006 8:46 AM
> 
> Ben Discoe wrote:
> > Another option is for me to open the file first, e.g. for Windows:
> > 	std::wstring fname;
> > 	FILE *fp = _wfopen(fname, L"rb");
> --------------------------^^^^^^^
> 
> A side note, there is no implicit conversion defined
> You need to use fname.c_str().

You are correct, sorry i left that out.  The code would be like this:

FILE *GDALFileOpen(const char *fname_utf8, const char *mode)
{
#if WIN32	// Windows
	wstring2 fn, mo(mode);
	fn.from_utf8(fname_utf8);
	return _wfopen(fn.c_str(), mo.c_str());
#elif __DARWIN_OSX__	// Mac OS X
	return fopen(fname_utf8, mode);
#else	// some other Unix flavor
	wstring2 fn, mo(mode);
	fn.from_utf8(fname_utf8);
	return fopen(fn.mb_str(), mode);
#endif
}

This is using the small "wstring2" class which is just std::wstring plus
methods for converting to and from utf-8 and mbs.

The problem i have _not_ solved is what to do on Windows with code which
uses std::ifstream() instead of fopen().  MSDN documents that there is a
(wchar_t*) version of std::ifstream() constructor
(http://msdn2.microsoft.com/en-us/library/zek0beca.aspx), but it does not
appear in the headers/libs that come with MSVC 7.1.  Fortunately this should
not affect GDAL, since AFAIK it does a good job of avoiding the C++ stream
classes. (Correct?)

-Ben




More information about the Gdal-dev mailing list