For Programmers: Free Programming Magazines  


Home > Archive > VC Language > January 2006 > istringstream not working the same in Visual Studio 2005









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author istringstream not working the same in Visual Studio 2005
schedman

2006-01-24, 4:00 am

In moving some code from VS2003 to VS2005 I have an execution error which I
traced to the following example code (Win 32 Console app)
#include "stdafx.h"
#include <sstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int nTmp=-3;
char cTmp1= '0';
char cTmp2= '0';
char buf[]= "{5, ";
istringstream ist(buf);
ist >> cTmp1;
ist >> nTmp;
ist >> cTmp2;

return 0;
}
If compiled with VS2003 you get cTmp1= { nTmp= 5 cTmp2= ,
which is the desired result.

in VS2005 the nTmp value remains = -3. It appears that the >> operation can
not
handle an int, combination. int , is ok.

Is there a workaround that does not require the input data to be modifed?

Tom Widmer [VC++ MVP]

2006-01-24, 4:00 am

schedman wrote:
> In moving some code from VS2003 to VS2005 I have an execution error which I
> traced to the following example code (Win 32 Console app)
> #include "stdafx.h"
> #include <sstream>
> using namespace std;
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> int nTmp=-3;
> char cTmp1= '0';
> char cTmp2= '0';
> char buf[]= "{5, ";
> istringstream ist(buf);
> ist >> cTmp1;
> ist >> nTmp;
> ist >> cTmp2;
>
> return 0;
> }
> If compiled with VS2003 you get cTmp1= { nTmp= 5 cTmp2= ,
> which is the desired result.
>
> in VS2005 the nTmp value remains = -3. It appears that the >> operation can
> not
> handle an int, combination. int , is ok.
>
> Is there a workaround that does not require the input data to be modifed?


Looks like the infamous locales problem. Try adding:

std::locale::global(std::locale::classic
());

at the start of your program. I think the problem is that the program is
picking up ',' as a special character for ints, presumably the thousands
separator, due to not running in the "C" locale, but rather I think it
has picked up the Windows locale.

Tom
P.J. Plauger

2006-01-24, 8:00 am

"Tom Widmer [VC++ MVP]" <tom_usenet@hotmail.com> wrote in message
news:%23cs%23ixMIGHA.596@TK2MSFTNGP10.phx.gbl...

> schedman wrote:
>
> Looks like the infamous locales problem. Try adding:
>
> std::locale::global(std::locale::classic
());
>
> at the start of your program. I think the problem is that the program is
> picking up ',' as a special character for ints, presumably the thousands
> separator, due to not running in the "C" locale, but rather I think it has
> picked up the Windows locale.


I'm afraid it's worse than that. It's a collision of two "under the radar"
changes
to the C++ Standard that have slowly become apparent through changes in
validation suites. The first is a requirement that the thousands separator
be
reported as comma *even in the "C" locale*, where it has long been "no
character". We put that in for V8 (Visual Studio 2005) and discovered
belatedly that the code now eats trailing commas. Turns out, however,
that another sub rosa change, in the handling of grouping, tells us not to
eat that comma; but that change didn't make it into V8. I believe this
issue popped up on this NG last December.

Bottom line: The fix is to change one line in _Getifld in <xlocnum> from:

const _Elem _Kseparator = _Punct_fac.thousands_sep();

to:

const _Elem _Kseparator = _Grouping.size() == 0
? (_Elem)0 : _Punct_fac.thousands_sep();

This defeats looking for separators if no grouping is specified.

The same fix is required in _Getmfld in <xlocmon>.

I believe Microsoft plans to patch this sooner rather than later, because
it's such a nuisance.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


John Carson

2006-01-24, 8:00 am

"P.J. Plauger" <pjp@dinkumware.com> wrote in message
news:GtKdnY9qCfHOikveRVn-sg@giganews.com
>
> Bottom line: The fix is to change one line in _Getifld in <xlocnum>
> from:
> const _Elem _Kseparator = _Punct_fac.thousands_sep();
>
> to:
>
> const _Elem _Kseparator = _Grouping.size() == 0
> ? (_Elem)0 : _Punct_fac.thousands_sep();
>
> This defeats looking for separators if no grouping is specified.
>
> The same fix is required in _Getmfld in <xlocmon>.



Thanks for the information on this.

For those of us who are a little slow, could you be explicit about exactly
what the second fix is. I looked for _Getmfld in <xlocmon> for Visual C++
Express 2005 and, while I found _Getmfld, I couldn't find it in any
expression that looks like the one that _Kseparator is in.


--
John Carson


P.J. Plauger

2006-01-24, 7:07 pm

"John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:u7ZrrwOIGHA.2064@TK2MSFTNGP09.phx.gbl...

> "P.J. Plauger" <pjp@dinkumware.com> wrote in message
> news:GtKdnY9qCfHOikveRVn-sg@giganews.com
>
>
> Thanks for the information on this.
>
> For those of us who are a little slow, could you be explicit about exactly
> what the second fix is. I looked for _Getmfld in <xlocmon> for Visual C++
> Express 2005 and, while I found _Getmfld, I couldn't find it in any
> expression that looks like the one that _Kseparator is in.


Sorry, it is a bit messier. Look for the code sequence:

case money_base::value:
{ // parse value field
int _Fracdigseen = 0;
int _Fracdigits = _Ppunct_fac->frac_digits();
const _Elem _E0 = _MAKLOCCHR(_Elem, '0', _Cvt);
const string _Grouping = _Ppunct_fac->grouping();

if (CHAR_MAX <= (unsigned char)*_Grouping.c_str())
for (; _First != _Last
&& _E0 <= *_First && *_First <= _E0 + 9; ++_First)
_Val += *_First; // no grouping, just gather digits
else
{ // grouping specified, gather digits and group sizes
const _Elem _Kseparator = _Ppunct_fac->thousands_sep();
string _Groups((size_t)1, '\0');
size_t _Group = 0;

and change it to:

case money_base::value:
{ // parse value field
int _Fracdigseen = 0;
int _Fracdigits = _Ppunct_fac->frac_digits();
const _Elem _E0 = _MAKLOCCHR(_Elem, '0', _Cvt);
const string _Grouping = _Ppunct_fac->grouping();
const _Elem _Kseparator = _Grouping.size() == 0
? (_Elem)0 : _Ppunct_fac->thousands_sep();

if (_Kseparator == (_Elem)0
|| CHAR_MAX <= (unsigned char)*_Grouping.c_str())
for (; _First != _Last
&& _E0 <= *_First && *_First <= _E0 + 9; ++_First)
_Val += *_First; // no grouping, just gather digits
else
{ // grouping specified, gather digits and group sizes
string _Groups((size_t)1, '\0');
size_t _Group = 0;

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


red floyd

2006-01-24, 7:07 pm

P.J. Plauger wrote:
> [patch descriptions redacted]
>
> P.J. Plauger
> Dinkumware, Ltd.
> http://www.dinkumware.com


Will these be be posted on the Dinkumware web site, similar to the way
that VC6 library patches were?

schedman

2006-01-24, 7:07 pm

Thanks for the quick replies.
I tried the std::locale::global(std::locale::classic
());
it did not work (as anticipated by P J Plauger). I have found the place to
modify xlocmon but I have never patched microsoft code before. It seems I
need to change some file protection to edit things, and I gues I need to
recompile it.

Is there a proccedure for this?

Bill Fisher (schedman)

"red floyd" wrote:

> P.J. Plauger wrote:
>
> Will these be be posted on the Dinkumware web site, similar to the way
> that VC6 library patches were?
>
>

P.J. Plauger

2006-01-24, 7:07 pm

"red floyd" <no.spam@here.dude> wrote in message
news:OU3SaAQIGHA.596@TK2MSFTNGP10.phx.gbl...

> P.J. Plauger wrote:
>
> Will these be be posted on the Dinkumware web site, similar to the way
> that VC6 library patches were?


No. We don't want to trip over Microsoft's own support machinery.
When we put up the V6 patches, many years ago, things were rather
different.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


David Wilkinson

2006-01-24, 7:07 pm

schedman wrote:

> Thanks for the quick replies.
> I tried the std::locale::global(std::locale::classic
());
> it did not work (as anticipated by P J Plauger). I have found the place to
> modify xlocmon but I have never patched microsoft code before. It seems I
> need to change some file protection to edit things, and I gues I need to
> recompile it.
>


schedman:

The best way to patch any of the include files that come with your VC
installation is to copy the offending files to a different folder, and
edit them there. Then put this folder above all the VC includes in your
VC Include directory settings (Tools, Options, Projects, VC++
Directories). That way, you can always go back by moving your new folder
to the bottom of the list.

David Wilkinson
schedman

2006-01-24, 7:07 pm

P.J Plauger, all
I gave the modification a try. I place the modified file Xlocmon in another
folder and changed the order as David Wilkerson sugested. I can see that the
modified h file is used by placing an #include <xlocmon> at the begining of
the program and having VS show me the source code.

Unfortunately I still get -3 for nTmp.

I looked at the code called by the >> and it appears that there is a step
in a file xlocnum (l602) that is dealing with _Ksepartor. but I can't follow
what is going on.
It does not appear that code in xlocmon.

any thoughts?





"P.J. Plauger" wrote:

> "red floyd" <no.spam@here.dude> wrote in message
> news:OU3SaAQIGHA.596@TK2MSFTNGP10.phx.gbl...
>
>
> No. We don't want to trip over Microsoft's own support machinery.
> When we put up the V6 patches, many years ago, things were rather
> different.
>
> P.J. Plauger
> Dinkumware, Ltd.
> http://www.dinkumware.com
>
>
>

P.J. Plauger

2006-01-24, 7:07 pm

"schedman" <schedman@discussions.microsoft.com> wrote in message
news:684159AE-443C-4BAA-95C9-DD90E986910F@microsoft.com...

> P.J Plauger, all
> I gave the modification a try. I place the modified file Xlocmon in
> another
> folder and changed the order as David Wilkerson sugested. I can see that
> the
> modified h file is used by placing an #include <xlocmon> at the begining
> of
> the program and having VS show me the source code.
>
> Unfortunately I still get -3 for nTmp.
>
> I looked at the code called by the >> and it appears that there is a step
> in a file xlocnum (l602) that is dealing with _Ksepartor. but I can't
> follow
> what is going on.
> It does not appear that code in xlocmon.
>
> any thoughts?


Are you linking dynamically or statically. If the former, you have to
rebuild
the supplied DLL (not an easy chore).

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


schedman

2006-01-24, 7:07 pm



"P.J. Plauger" wrote:

> "schedman" <schedman@discussions.microsoft.com> wrote in message
> news:684159AE-443C-4BAA-95C9-DD90E986910F@microsoft.com...
>
>
> Are you linking dynamically or statically. If the former, you have to
> rebuild
> the supplied DLL (not an easy chore).
>
> P.J. Plauger
> Dinkumware, Ltd.
> http://www.dinkumware.com
>

When I create dlls I generally use static dlls but in this case everything
I created is in main. There is only a few lines of code. I don't know how to
tell if the microsoft code is static. I looked at the VS2005 documentation
but that did not seem to answer your question. I accepted the MS VS2005
defaults for a Win32 Console client.

Bill Fisher (schedman)
>
>

Carl Daniel [VC++ MVP]

2006-01-24, 9:58 pm

schedman wrote:
> "P.J. Plauger" wrote:
>
> When I create dlls I generally use static dlls but in this case
> everything I created is in main. There is only a few lines of code.
> I don't know how to tell if the microsoft code is static. I looked at
> the VS2005 documentation but that did not seem to answer your
> question. I accepted the MS VS2005 defaults for a Win32 Console
> client.


With VS 2005, projects use the DLL version of the CRT (and the STL) by
default, so you're using the DLL version.

-cd


schedman

2006-01-25, 4:17 am



"Carl Daniel [VC++ MVP]" wrote:

> schedman wrote:
>
> With VS 2005, projects use the DLL version of the CRT (and the STL) by
> default, so you're using the DLL version.
>
> -cd
>
> Is it possible to override this behavior to test the change above, or is there a poceeddure for recompiling xlocmon.


Also has Microsoft set a date for when this issue might be patch?
Thanks
>

John Carson

2006-01-25, 4:17 am

"schedman" <schedman@discussions.microsoft.com> wrote in message
news:4DAC2EC9-6950-4FB8-8680-0866352CB9A7@microsoft.com[color=darkred]
> "Carl Daniel [VC++ MVP]" wrote:

Go to

Project->Properties->Configuration Properties->C/C++ ->Code Generation

and make the appropriate selection in Runtime Library (you want /MT or /MTd
instead of /MD or /MDd).


--
John Carson


Carl Daniel [VC++ MVP]

2006-01-25, 7:23 pm

schedman wrote:
> Also has Microsoft set a date for when this issue might be patch?


They've announced that there will be a service pack for VS 2005 in the first
1/2 of this year. If you want to increase the chances that this issue will
be addressed in the service pack, please go to

http://lab.msdn.microsoft.com/productfeedback

and report the bug. Bugs reported by customers through this mechanism are
far more likely to be fixed in a service pack. You should mention this
newsgroup thread in the bug report:

http://groups.google.com/group/micr...6c084d09bf204is the Url for this thread from Google Groups.-cd

schedman

2006-01-25, 7:23 pm

Hello all
Thanks for your help,
My settings were /MTd, still didn't do it. I am going to use up one of my
MSDN incidents to work this.
I will post when(if) I have a solution.

"Carl Daniel [VC++ MVP]" wrote:

> schedman wrote:
> They've announced that there will be a service pack for VS 2005 in the first
> 1/2 of this year. If you want to increase the chances that this issue will
> be addressed in the service pack, please go to
>
> http://lab.msdn.microsoft.com/productfeedback
>
> and report the bug. Bugs reported by customers through this mechanism are
> far more likely to be fixed in a service pack. You should mention this
> newsgroup thread in the bug report:
>
> http://groups.google.com/group/micr...6c084d09bf204is the Url for this thread from Google Groups.-cd
>
>

schedman

2006-01-27, 7:05 pm

An update. The MS product team has decided this is a bug with no workaround.
They a considering oit for a Quick Fix Engineering. Don't know what the
chances are; At least it is on the radar.


"schedman" wrote:
[color=darkred]
> Hello all
> Thanks for your help,
> My settings were /MTd, still didn't do it. I am going to use up one of my
> MSDN incidents to work this.
> I will post when(if) I have a solution.
>
> "Carl Daniel [VC++ MVP]" wrote:
>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com