Jump to content
  • Advertisement
Sign in to follow this  
Skeleton_V@T

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

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

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!