May 8, 2013 at 8:00am UTC
let says i have
char *yytext = "p103_stb" 0r "p0_stb" or "p100_stb"
How to do substr to get the "_stb" in C?
May 8, 2013 at 8:40am UTC
well, I would use std::string for this, unless of course you mean "pure" c, and not c++.
May 8, 2013 at 8:47am UTC
2 3 4 5 6
string vre_pin = yytext;
"chunhaun1:" << vre_pin <<endl;
unsigned position = vre_pin.find( "_stb");
cout << "pos:" << position <<endl;
string is_stb = vre_pin.substr(position);
cout << "chunhaun2:" << is_stb << endl;
Hi this is what i tried using c++.but error occur.could you help me check with my problem?The example of string will be just same as above.
Why my yytext content are either the axample above when i print vre_pin.size() i got all the value with 4294967295?
terminate called after throwing an instance of 'std::out_of_range'
Last edited on
May 8, 2013 at 9:12am UTC
May 8, 2013 at 9:42am UTC
I am working with lex together. So when my yytext match the string when lex scan thru a file.
so yytext will read during run time.Therefore,I cannot direct assign string to yytext.
But i wonder why when i print out the position will appear out of range error.
Or should i do somethg like chomp off space,newline after the string just like perl.
May 8, 2013 at 10:02am UTC
Ya.i print out the position all is the value 4294967295.
I did try your code.It happened the same. :(
I am using g++ in unix to compile.
May 8, 2013 at 10:08am UTC
hmm, well, not sure why this is happening. my ide is visual 2012, and the code I posted works. maybe someone else has any input??
May 8, 2013 at 11:15am UTC
Last edited on
May 8, 2013 at 11:20am UTC
May 8, 2013 at 2:03pm UTC
Same problem as with the string::find : if strstr returns null pointer, then calling strcmp is not safe.