We help IT Professionals succeed at work.

How do I parse this CString?

shaolinfunk
shaolinfunk asked
on
I have a CString s that outputs to a text file like so:  07:14:18

The code is below:

CTime ct(Doodad->GetEventTime());
CString s = ct.Format("%H:%M:%S");

7 is the hour, 14 is the minute, and 18 is the seconds.

How do I parse this CString so that 7,14,18 are entered into integer variables intHour, intMinute, intSecond?

Thanks.
Comment
Watch Question

Top Expert 2012
Commented:
Well, 'sscanf()' could do that like

sscanf((LPCTSTR)s,"%d:%d%d",&intHour,&intMinute,intSecond);

but it's be way easier to use your 'CTime' variable, e.g.
CTime ct(Doodad->GetEventTime());

int intHour   = ct.GetHour();
int intMinute = ct.GetMinute();
int intSecond = ct.GetYSecond();

Open in new window

jkr
Top Expert 2012

Commented:
Sorry, typo - that should be
CTime ct(Doodad->GetEventTime());

int intHour   = ct.GetHour();
int intMinute = ct.GetMinute();
int intSecond = ct.GetSecond();

Open in new window

jkr
Top Expert 2012

Commented:
Just in case you really want to parse the CString in a C++ way:
#include <sstream>
using namespace std;

//...

CTime ct(Doodad->GetEventTime());
CString s = ct.Format("%H:%M:%S"); 

int intHour;
int intMinute;
int intSecond;
char c; // a 'dummy' for grabbing ':'

stringstream ss((LPCTSTR)s);

ss >> intHour >> c >> intMinute >> c >> intSecond;

Open in new window

Author

Commented:
thanks jkr!  i like the .GetHour, etc. way to do it.