473,804 Members | 3,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing a string into integers using istringstream

I want to parse a string (actually a date [always in the format
'YYYYMMDD') into integer values like this:

void parseDate(const string&sdate, int& day, int& mon, int &year)
{
istringstream iss(sdate) ;
....
}

I can do this easily in C (using atoi etc), but i wanted not too sure
how in C++.

Nov 6 '06 #1
2 3446
Bit Byte wrote:
I want to parse a string (actually a date [always in the format
'YYYYMMDD') into integer values like this:

void parseDate(const string&sdate, int& day, int& mon, int &year)
{
istringstream iss(sdate) ;
....
}

I can do this easily in C (using atoi etc), but i wanted not too sure
how in C++.
First you need to convert "YYYYMMDD" into components.

std::string date = "20061113";

// Take care to assert here if the string is not long enough.
assert( date.size() == 8 );

std::string year( date.begin(), date.begin() + 4 );

std::string month( date.begin() + 4, date.begin() + 6 );

std::string day( date.begin() + 6, date.end() );

Then you can convert each component.
Nov 6 '06 #2
Gianni Mariani wrote:
Bit Byte wrote:
I want to parse a string (actually a date [always in the format
'YYYYMMDD') into integer values like this:

void parseDate(const string&sdate, int& day, int& mon, int &year)
{
istringstream iss(sdate) ;
....
}

I can do this easily in C (using atoi etc), but i wanted not too
sure how in C++.

First you need to convert "YYYYMMDD" into components.

std::string date = "20061113";

// Take care to assert here if the string is not long enough.
assert( date.size() == 8 );

std::string year( date.begin(), date.begin() + 4 );

std::string month( date.begin() + 4, date.begin() + 6 );

std::string day( date.begin() + 6, date.end() );

Then you can convert each component.

Yikes. This is a case where'd I'd just use old-style:
sscanf(sdate.c_ str(), "%4d%2d%2d" , &year, &mon, &day);
There's probably some Boost thing that does something similar, but I
don't know anything about that.


Brian
Nov 6 '06 #3

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

Similar topics

10
1902
by: george young | last post by:
For each run of my app, I have a known set of (<100) wafer names. Names are sometimes simply integers, sometimes a short string, and sometimes a short string followed by an integer, e.g.: 5, 6, 7, 8, 9, bar, foo_6, foo_7, foo_8, foo_9, foo_10, foo_11 I need to read user input of a subset of these. The user will type a set of names separated by commas (with optional white space), but there may also be sequences indicated by a dash...
10
2639
by: Christopher Benson-Manica | last post by:
(if this is a FAQ, I apologize for not finding it) I have a C-style string that I'd like to cleanly separate into tokens (based on the '.' character) and then convert those tokens to unsigned integers. What is the best standard(!) C++ way to accomplish this? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
6
18527
by: Allan Bruce | last post by:
I have a string like: "FL:1234ABCD:3:FileName With Spaces.txt\n" and I want to read the values separated by ':' into variables. I tried to use sscanf like this: sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName); but the lGUID just continues until whitespace or \0 is found. I need a way
5
141933
by: Steve | last post by:
Hi, How can I convert a C++ string to an integer and then an integer back to a string? Is there built-in support for this? Thanks, Steve
10
2270
by: Randy Yates | last post by:
How to do? Such a simple #@%^&@ thing and I'm spending hours on it. For example, long mynum; istringstream mystr("380,900", istringstream::in); mystr >> mynum; cout << mynum;
12
5622
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a window and hit a button to process it. The information thus enters the program as a char array. Prices can be between $1 and $100, including cents. So we can have prices such as 3.01, 1.56, 11.57, etc. The char array is an alphanumeric...
1
7034
by: Adam Parkin | last post by:
Hello all, I'm trying to write a function which given a std::string parses the string by breaking the sentance up by whitespace (\t, ' ', \n) and returns the result as a vector of strings. Here's what I have so far: std::vector<std::string> tokenize (std::string foo) { std::istringstream s (foo); std::vector <std::string> v; std::string tok;
2
1881
by: Adrian | last post by:
Hi All, Is there anyway to change what isspace thinks is a space character. I am parsing some log files and it would be nice to just read a field as what ever is between quotes or between 's ie clf log files I know I can go char by char or find_last_of etc, but I would like to know if it is possible with locales and facets? Also are there any suggestions for outputting in multiple languages? Can I use locales again? I was just...
5
1899
by: tech | last post by:
Hi, I need to parse a string used to represent a time and then populate a simple time struct. The time string will always be this format 23:45.45 ie hours separated from mins by ':' and minutes separated from seconds by '.' The string will be 8 chars in len. I've come up with some simple code below but am wondering do i really need a wstringstream and a string to do this. Can
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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,...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
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
10076
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...
1
7616
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
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
3816
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.