Can someone help me understand this piece of code:
char *line = new char[2048];
char *probableCauseStr = new char[512];
char *additioanl_text = new char[512];
long holdPeriod = 0;
while( !f.eof() ) {
f.getline( line, 2048 );
//
// find the ',' seperator
//
char* p = StrMgt::strchr( line, ',' );
if( !p ) continue;
*p = '\0';
p++;
if( sscanf( line, "%s%s", probableCauseStr, additioanl_text ) != 1 ||
sscanf( p, "%ld%s", &holdPeriod, additioanl_text ) != 1 ) continue;
....
I’m lost trying to figure out what happens with the character pointer p.
pis used to find the first comma and replace it with\0(which is the end-of-string for C-style strings, in particular for sscanf). Thenpis incremented to point at the next character.So a string like
becomes
Then the two sscanfs are tried, checking for whichever one returns 1 (which means that it parsed exactly 1 value). In this example, the first sscanf would return 2 (since there are two words), so the second one will be called, and will return 1, with
holdPeriodgetting the value100000.