473,320 Members | 1,978 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,320 software developers and data experts.

wistringstream wierd problem (but istringstream works)

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.
Jul 1 '08 #1
3 2255
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?

Jul 1 '08 #2
On Jul 1, 7:41*pm, Angus <anguscom...@gmail.comwrote:
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).
Jul 1 '08 #3
Linlin Yan <ya*********@gmail.comwrote in news:ed40debc-f2b0-422c-b6c1-
11**********@c19g2000prf.googlegroups.com:
On Jul 1, 7:41*pm, Angus <anguscom...@gmail.comwrote:
>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).
This works for me in VC++ 8.0 as well.

joe
Jul 1 '08 #4

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

Similar topics

1
by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the...
3
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test...
8
by: Frank Nielsen | last post by:
Can istrstream contain binary data like '\0' and other non ascii characters???? My program received a byte array through jni (java) and i must put it in a stream to continue the flow in the...
7
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
4
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output...
6
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
11
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
7
by: mathieu | last post by:
Hello, Is there a way to construct a std::istringstream from a buffer of char and avoid copying the array of bytes ? const char s = "Hello, World"; std::string str(s); std::istringstream is;...
2
by: rolan | last post by:
I'm trying to figure out how to convert an array of type "string" into integers, and I have begun using istringstream. I made a program to test how istringstream works, and I've been having...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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)...
0
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.