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

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 3842
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*****@hotmail.com> wrote in message
news:c0rtc.77772$hH.1441703@bgtnsc04-
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*****@hotmail.com> wrote in message news:<c0********************@bgtnsc04-news.ops.worldnet.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::facet. 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*****@hotmail.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
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
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...
4
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...
4
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...
3
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
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...
2
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
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...
4
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 <<...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.