473,769 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

read fixed width numbers from stringstream

Is it possible to convert 2 characters from a stringstream to an integer without
using an intermediate a 2-bytes string ? The following fragment doesn't work

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
istringstream lb("200");
int ct;
lb>>setw(2)>>ct ;
cout<<ct;
return 0;
}
Jul 22 '05 #1
7 3886
In article <40************ ***@t-online.de>,
klaus hoffmann <dr***********@ t-online.de> wrote:
Is it possible to convert 2 characters from a stringstream to an integer
without
using an intermediate a 2-bytes string ? The following fragment doesn't work

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
istringstream lb("200");
int ct;
lb>>setw(2)>>ct ;
cout<<ct;
return 0;
}


Yes. You can derive from std::num_get and override the appropriate
do_get virtual functions to read/convert 2 digit integers. Then you
need to create a locale with this derived facet and imbue your
istringstream with that locale.

Having implemented num_get myself, if I needed to do what you're doing,
I'd just use an intermediate 2-byte string.

-Howard
Jul 22 '05 #2
Howard Hinnant schrieb:

In article <40************ ***@t-online.de>,
klaus hoffmann <dr***********@ t-online.de> wrote:
Is it possible to convert 2 characters from a stringstream to an integer
without
using an intermediate a 2-bytes string ?


Yes. You can derive from std::num_get and override the appropriate
do_get virtual functions to read/convert 2 digit integers. Then you
need to create a locale with this derived facet and imbue your
istringstream with that locale.

Having implemented num_get myself, if I needed to do what you're doing,
I'd just use an intermediate 2-byte string.

-Howard


Thank you. So it seems okay to even use sscanf in this case.
sicerely
Klaus
Jul 22 '05 #3
> ... Then you
need to create a locale with this derived facet and imbue your
istringstream with that locale.


Hi Howard,

this is the other Howard. :-)

I'm not sure what that sentence above means. What's a "facet"? And
what's a "locale"? And how do you "imbue" a stream with it? Sorry, but I
just don't recognize those terms (at least not in the context of C++
programming).

Thanks,
-Howard
Jul 22 '05 #4
"Howard" <al*****@hotmai l.com> wrote in message
news:c0rtc.7777 2$hH.1441703@bg tnsc04-
I'm not sure what that sentence above means. What's a "facet"? And
what's a "locale"? And how do you "imbue" a stream with it? Sorry, but I
just don't recognize those terms (at least not in the context of C++
programming).


See the thread "convert hex to oct" where I gave a full example of how to
make a stream that interprets the comma as a space character, and thus you
can read "1,2,3" as stream >> x >> y >> z. It shows locales, facets, imbue.

Now I was playing around with the idea of locales and facets in order to see
how to use them. I have yet to use it in real code. Sometimes the locale
and facet thing seems overkill, and leads to obstruse semantics. On the
other hand, anything that's new seems this way at first, so I'm still trying
to keep an open mind to determine when and when not to use locales and
facets.

You can also learn about locales and facets in the later chapters of the
standard (which was my reference guide in writing the code), Stroustrup's
book. And I think PJ Plauger had lots of columns about these in a C++
magazine. If you can find those, they'd probably be the best reference.
Jul 22 '05 #5
"Howard" <al*****@hotmai l.com> wrote in message news:<c0******* *************@b gtnsc04-news.ops.worldn et.att.net>...

[ ... ]
I'm not sure what that sentence above means. What's a "facet"? And
what's a "locale"? And how do you "imbue" a stream with it? Sorry, but I
just don't recognize those terms (at least not in the context of C++
programming).


A locale describes how a set of characters in a character set should
be treated, interpreted, etc.

A locale consists of a number of facets -- each facet describes,
well...one facet of characters in that set. One facet describes the
basics of characters, such as which characters are considered letters,
digits, white-space, etc., in that character set. There are also
facets for how to read/write numbers, time values, how characters are
sorted, etc.

In your case, you want some custom processing done in reading numbers
from a stream. To do that, you start by creating a num_get facet,
which is object of a class derived (indirectly) from
std::locale::fa cet. It'll have to include some functions that take
input characters and convert them to numbers.

Using that is done in two steps. Each iostream has an associated
locale. Therefore, you have to 1) create a locale, and 2) tell the
istream to use that locale.

To create the locale, you usually specify a locale that uses the
facets from the default locale for everything except the thing(s) you
care about -- in this case, reading numbers. std::locale has a ctor
to do exactly that.

Having created a complete locale that uses your routines in its
num_get facet, you then have to tell the stream to use that locale --
this is done with a stream member function named imbue, so it's
generally called imbuing the stream with the locale.

This is a part of the library that many books completely or partly
skip over. There's a fair amount of "stuff" that you have to learn
before it all makes sense, and some of it perplexing even after you've
studied it (I'm pretty sure I still don't entirely understand codecvt
facets) but quite a bit of it is fairly easy to use once you learn
about it, and it really can make some tasks a lot easier than would
otherwise be the case. In particular, they allow you to modify
specific parts of how input or output is done in isolation. OTOH,
some (as Howard Hinnant hinted at) are difficult to get right -- he's
clearly a smart guy, and doesn't shrink away from doing some hard
programming when needed, so I (for one) wouldn't blithely ignore it
when he hints that someting is particularly difficult.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #6

"Jerry Coffin" <jc*****@taeus. com> wrote in message

Thanks! That's very good information. I'd heard of locales before, but
didn't see how they applied to the OP's question at all. (I'd only seen
them used as a way to represent the characters of a particular language or
dialect, and hadn't ever looked into the details of using them.)

Thanks for the info...

-Howard (the other Howard)


Jul 22 '05 #7
On Tue, 01 Jun 2004 14:33:12 GMT, "Howard" <al*****@hotmai l.com>
wrote:

"Jerry Coffin" <jc*****@taeus. com> wrote in message

Thanks! That's very good information. I'd heard of locales before, but
didn't see how they applied to the OP's question at all. (I'd only seen
them used as a way to represent the characters of a particular language or
dialect, and hadn't ever looked into the details of using them.)

Thanks for the info...


There's lots about it here:
http://www.research.att.com/~bs/3rd_loc0.html

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #8

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

Similar topics

26
9700
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " 4". Six spaces than the value of intQuantity. This is correct. But all the others end up being string objects of only 6 characters long (with the exception of strTotal). The left most positions of the string object are being padded with one...
4
7852
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places = 4> class FixedFloat {
4
5814
by: Martin Hvidberg | last post by:
Dear group I need to make a very simple piece of code in C, that can be command line executed and will compile on Linux, i.e. gcc. It should read a ascii Comma Separated Values (CSV) file and write the data (numbers) to another ascii file in a specific fixed width format (fwf). Let me show an example. >>> Infile.csv
4
6130
by: costantinos | last post by:
Hello. I have a string which looks like and I need to read these numbers. The logical implementation was to read the string char by char and check whether I have a space and when I find one I could create a vector with all the numbers (after i convert them to number of course). the problem is that when i am trying to do so I have errors with both implementations that i have tried. for(my_iter = mla.begin(); my_iter != mla.end();...
3
4961
by: computerwolf8 | last post by:
I have a file where I know the lines go as follows: string long string int int string double
0
1716
by: heplesser | last post by:
Summary: Does the C++ standard require std::basic_istream<>::operator>>(double&) to leave the input stream untouched in case of a read failure? Details: I noticed an unexpected behavior of operator>>() for numbers (double, int) when reading from cin. I would like to ask for expert clarification on whether I am misunderstanding the rules of the game, or whether my library implementation has a bug. I tested this on g++
2
3280
by: zredcard | last post by:
so far ive got int width; int height; int bitdepth; stringstream ss; string str; typedef unsigned char byte int _tmain(int argc, _TCHAR* argv) {
6
5937
by: Steven Woody | last post by:
Hi, for example, i have unsigned char buff the buffer hold a sequences of raw bytes. My question is, what is the standard method of reading the `buff' via std::istream interface? A relative question is: how to write to the `buff' via std::ostream
4
8720
by: Johannes Bauer | last post by:
Hello group, I've a simple problem with the precision specifiers of stringstream. Let's say I have this: double x = 123.4567890; std::stringstream Strm; Strm.precision(4); Strm << std::scientific << x; std::cerr << Strm.str() << std::endl;
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.