std::basic_*fstream lacks of supporting Unicode file names ?

Started by
12 comments, last by Skeleton_V@T 18 years, 3 months ago
Hi. The title has described most of my problem. My OpenGL project has been built to be compiled in both ANSI and Unicode strings friendly. I've been using standard C I/O functions to deal with files, they are _open (), _read (), and _write (). So far so good, everything has been working properly. Recently I've decided to replace all of these old functions by the C++ IOStream library objects, specifically by the std::basic_*fstream. The problem is that while the class accepts any types and customizable character traits thrown to it, the constructor seems to be capable of handling char const* file names only, which is unconvenient if I want to pass some Unicode files to it. I've been searching for similar problems but all I've found is a (possibly not in the standard library) *fstream::attach () which takes an argument of a file descriptor from standard C _open () and _wopen () functions. I couldn't find it in VS 2003's shipped STL implementation. Are CPP IOStream classes able to handle Unicode files ?. Thank you for your helps.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Advertisement
Isn't there a wfstream for Unicode strings in the STL?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_fstream_wfstream.asp
Quote:Original post by Joni-Matti
Isn't there a wfstream for Unicode strings in the STL?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_fstream_wfstream.asp


You're right, but my problem lays on the constructor (in open () member function as well), where it doesn't take a unsigned short const* file name, as usually in a Unicode name, but char const* instead. After all, wfstream is a typedef of basic_fstream<wchar_t>, which I'm already able to handle.

I'll resort to my good old C IO functions if there's no other way around (fortunately there is _wopen () function for this). Thanks for your help anyway.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
I would go for FILE* and _wfopen() then. Of course you can use Win32 API's CreateFileW, but I myself don't like the Win32 reading routines.
The reason I want to switch to the C++ IOStream is of the fact that it is more exception-safe and has very nice overloaded operators, I don't have to remember to _close () the file either.

Also from my experiences, the standard IOStream can not handle virtual arguments. I used to use those *sprintf () and v*sprintf () functions, which is not a nice mixture between C++ std::iostream and C sprintf () function.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Quote:Original post by Skeleton_V@T
I've been using standard C I/O functions to deal with files, they are _open (), _read (), and _write ().


FWIW, these are NOT C standard library functions.
Quote:Original post by Skeleton_V@T
Also from my experiences, the standard IOStream can not handle virtual arguments. I used to use those *sprintf () and v*sprintf () functions, which is not a nice mixture between C++ std::iostream and C sprintf () function.


What's a virtual argument?

Stephen M. Webb
Professional Free Software Developer

Quote:FWIW, these are NOT C standard library functions.

I've seen them shipped with many C compilers (from the first version of ancient TurboC to VS 2003) in their CRT libraries. Whether or not, they are deprecated now. But can you tell me where did they come from ?.

Quote:What's a virtual argument?

They are arguments that are not specified in the function declaration:
int Func1 (int const Arg1, ...) ;

The ellipses let the caller pass as much additional arguments as desired, the Func1 () function can then parse the argument Arg1 to decide how many arguments were actually passed by the caller. You may use va_arg, va_end and va_start macros to take these arguments.

This is the technique used in vsprintf (), printf () C functions to deal with arbitrary number of arguments. You may refer MSDN for more infos.

Does anyone know how to pass an Unicode string as file name to the open () or the constructor of std::basic_*fstream ?
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Quote:Original post by Skeleton_V@T
Quote:What's a virtual argument?

They are arguments that are not specified in the function declaration:
int Func1 (int const Arg1, ...) ;


That's not the terminology you want, sorry.

Naturally streams don't work with those because you normally interact with them using operators. Varargs can't support a lot of C++ types *anyway* (basically they only work with POD types), so this is done with very good reason.

Quote:This is the technique used in vsprintf (), printf () C functions to deal with arbitrary number of arguments. You may refer MSDN for more infos.


Iostreams deal with this by operator chaining. It works well and is type-safe.

As for Unicode files - you can deal with Unicode file *content* just fine with wfstreams. If the file *name* is Unicode, then you will probably not be able to find anything really portable at all in the standard library; try seeing if boost::filesystem and/or boost::iostreams contain what you need.
Thank you, at least you made the predication, I'll definitely check out boost's.

Actually this is part of my efforts of replacing all the old C functions to be more conformant with the C++ coding standard. I've learnt a lot about the C++ standard library and the STL as well since my last post (std::string). To be honest, I was a bit disappointed - with the fame of the standard library - as not all of the good 'ol C functions can be replaced. Perhaps that's why we've got the additional libraries such as boost ?.

Thanks for all your replies.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--

This topic is closed to new replies.

Advertisement