wistringstream wierd problem (but istringstream works)

A

Angus

int day;
std::string strDate = _T("01013000");
std::string strPart = strDate.substr(0, 2);
std::istringstream strm(strPart);
strm >> day;
// Great, day = 1

But...
int day;
std::wstring strDate = _T("01013000");
std::wstring strPart = strDate.substr(0, 2);
std::wistringstream strm(strPart);
strm >> day;
// Bad, day = 48

48 is ascii code for a zero. But why don't I het same behaviour as
for non-Unicode?

Same effect for the month and year. For the year I get 51 - ascii
code for 3.

What am I doing wrong?

In addition, another question on stringstream. If I use strm to get
the day then how do I clear strm to then get the month. I tried
strm.str(_T("")) and strm.clear - but neither worked in clearing
properly. So I was not able to get month.
 
A

Angus

I have just realised that my posted code was not quite how I had it.
Here is my code:

#include <sstream>
#include <string>

int main()
{

#ifdef _UNICODE
#define TEXT(quote) L##quote
#else // not UNICODE
#define TEXT(quote) quote
#endif // !UNICODE

#ifdef _UNICODE
typedef std::wstring tstring;
#else
typedef std::string tstring;
#endif

#ifdef _UNICODE
typedef std::wistringstream tistringstream;
#else
typedef std::istringstream tistringstream;
#endif

unsigned short day;
tstring strDate = TEXT("01013000");
tstring strPart = strDate.substr(0, 2);
tistringstream strm(strPart);
strm >> day;

return 0;
}

It doesn't work if day is an unsigned short. Can't work out why? If
day is defined as an int or a long it works. Something to do with the
overloading of types in istringstream?
 
L

Linlin Yan

I have just realised that my posted code was not quite how I had it.
Here is my code:

#include <sstream>
#include <string>

int main()
{

#ifdef _UNICODE
#define TEXT(quote) L##quote
#else   // not UNICODE
#define TEXT(quote) quote
#endif // !UNICODE

#ifdef _UNICODE
typedef std::wstring  tstring;
#else
typedef std::string   tstring;
#endif

#ifdef _UNICODE
typedef std::wistringstream  tistringstream;
#else
typedef std::istringstream   tistringstream;
#endif

    unsigned short day;
    tstring strDate = TEXT("01013000");
    tstring strPart = strDate.substr(0, 2);
    tistringstream strm(strPart);
    strm >> day;

    return 0;

}

It doesn't work if day is an unsigned short.  Can't work out why?  If
day is defined as an int or a long it works.  Something to do with the
overloading of types in istringstream?

I use g++ 4.2.3 (in Ubuntu) to build your code and run. Either using
type unsigned short or int can get the right value (day = 1).
 
J

Joe Greer

I use g++ 4.2.3 (in Ubuntu) to build your code and run. Either using
type unsigned short or int can get the right value (day = 1).

This works for me in VC++ 8.0 as well.

joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top