Re: [gtkmm] gstrings and file output



Am Son, 2002-08-25 um 00.15 schrieb Ollo:
> Hi.
> 
> Maybe someone can help me with this Unicode Problem.
> 
> I have a C++ class containing several Glib::usting objects and would
> like to save them to a file via an C++ STL ofstream.
> 
> To do so, it did the following:
> 
> In the Class containing the ustrings:
> 
> (1)
> friend std::basic_ostream<Glib::ustring::value_type>&
> 		operator<<(basic_ostream<Glib::ustring::value_type>& out, const Box& box)

Just use std::ostream "as is".  ustring::value_type is gunichar, which
is used for UCS-4 encoded characters as returned by e.g. operator[].

> {
> 		out << "partitions:" << box.m_partitions << std::endl;

This would convert the UTF-8 string to the current locale's encoding. 
If you don't want that (I assume so), use:

              out << box.m_partitions.raw();

> 		.....
> 		out << std::endl;
> }
> 
> And in the class doing the actual writing to the file
> 
> (2)
> blah blah ....
> {
> 	std::basic_ofstream<Glib::ustring::value_type> to("test.box")
> 	if (!to) std::cerr << "Cannot open file" << std::endl;
> 	to << *box;
> 	to.close();
> }
> 												
> If I use std::ostream, (1) gives me a conversion error from Glib,
> and if I use std::basic_ofstream<Glib::ustring::value_type>, (2) gives
> me a segmentation fault.

You shouldn't have got a conversion error -- converting UTF-8 to your
locale's encoding should work, unless something is really broken.  raw()
will suppress this problem, but could you mail us the exact error
message you got anyway?

> Can anybody tell me what I'm doing wrong??

Hopefully I just did so ;)

--Daniel





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]