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

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 3428
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
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,...
10
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...
6
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",...
5
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
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
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...
1
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...
2
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...
5
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...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.