473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

codecvt and stringstream

I implemented a codecvt facet (extracted from TCPL) but while it works well
with 'ifstream' it doesn't work at all with 'stringstream'.
Am I missing anything ? I tested it with MSVC 7.1

The code below shows what I'm trying to do:

#include <stdio.h>
#ifdef _WIN32
#include <io.h>
#else
#include <stdlib.h>
#endif

#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

class Cvt_to_upper : public codecvt<char, char, mbstate_t>
{
typedef codecvt<char, char, mbstate_tBase;

public:
explicit Cvt_to_upper(size_t refs = 0)
: Base(refs)
{
}

protected:
result do_in(state_type&,
const extern_type* from, const extern_type* from_end,
const extern_type*& from_next,
intern_type* to, intern_type* to_end,
intern_type*& to_next) const
{
while (from < from_end && to < to_end)
*to++ = toupper(*from++);

to_next = to;
from_next = from;
return from == from_end ? ok : partial;
}

bool do_always_noconv() const throw()
{
return false;
}
};

int main(int argc, char* argv[])
{

locale loc1;
locale loc (loc1, new Cvt_to_upper);

stringstream oss;
oss.imbue(loc);

char name[] = "charupperXXXXXX";
mktemp(name);

string lower_str("abcd");
string upper_str;

{
ofstream out(name);

out << lower_str;
oss << lower_str;
}
{
ifstream in;
in.imbue(loc);
in.open(name);

in >upper_str;
cout << "using ifstream: " << upper_str << std::endl;
oss >upper_str;
cout << "using stringstream: " << upper_str << std::endl;
}

remove(name);
}
Aug 8 '06 #1
1 3390
"FabioAng" <fa************@libero.itwrote in message
news:eb**********@nnrp-beta.newsland.it...
>I implemented a codecvt facet (extracted from TCPL) but while it works well
with 'ifstream' it doesn't work at all with 'stringstream'.
Am I missing anything ? I tested it with MSVC 7.1
Yes. Only basic_filebuf (used by ifstream) uses codecvt.
To use a codecvt facet with stringstream, you need something
like our wbuffer_convert. See:

http://www.dinkumware.com/manuals/?m...e=wbuffer.html

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Aug 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bill Beacom | last post by:
Hi all, I am trying to use stringstream to assemble a text message from a set of user-defined objects that have the insertion operator overloaded. It seems to be putting them into the...
2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
5
by: cherico | last post by:
I'd like to read stings from cin and put them in stringstream. I use a string object as an intermediate "container" to store data from cin and then put them in stringstream. stringstream ss ;...
5
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears...
2
by: JH Trauntvein | last post by:
I recently learned about the codecvt locale facet by reading Jossutis' (forgive me if I just misspelled his name) book on the C++ standard library (this book has been worth its weight in gold by...
8
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
1
by: interec | last post by:
Is it possible and ok to use boost::utf8_codecvt_facet to write a function to convert UTF-16 wchar_t to UTF-8 char and vice versa. boost::utf8_codecvt_facet...
1
by: Dancefire | last post by:
Hi, everyone, I'm trying to use std::codecvt<to do the encoding conversion. I am using following code for encoding conversion between wchar_t string and char string(MBCS). I am not sure am I...
6
by: wong_powah | last post by:
How to fix this compile error? # make -f Util/UtilBlock.dep g++ /opt/aCC/include_std/rw/codecvt.cc -o /opt/aCC/include_std/ rw/codecvt /opt/aCC/include_std/rw/codecvt.cc:32: error: use of...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.