let have string:
std::string s = "abcd\t1234";
i can use std::string::find
offset '\t' character, far know there no functions following signature:
int atoi_n(char *, int len);
an missing anything? strtok
replaces \t
\0
, , don't want touch original buffer. find hard believe there aren't instances of atoi
, atof
, etc take length parameter, can't find anything.
anyone know if there i'm missing? know boost has tokenizers i'd avoid having add dependency of boost.
looking comments far i'd clarify. let's change scenario: char buffer[1024]; char *pstartpos; char *pendpost; pstartpos = buffer + 5; pendpos = buffer + 10;
let's can't make assumptions memory outside pstartpos , pendpos. how convert charaters between pstartpos , pendpos int without adding '\0' buffer or copying using substr?
if want parse end of string (from character after \t
end) need pass pointer first character parse atoi
...
int n = atoi(s.c_str()+s.find('\t')+1);
(error checking omitted brevity - in particular, assuming \t
present)
if, instead, want parse beginning of string \t
can do
int n = atoi(s.c_str());
since atoi
stops @ first non-numeric character anyway.
by way, should consider using more robust solutions parsing number, strtol
, sscanf
or c++ streams - can report parsing error in way, while atoi
returns 0
(which isn't distinguishable 0 comes parsing string).
incidentally, atoi
not in "stl" means - it's part of c standard library.
i know atoi not in stl. wondering if there in stl can specify last character want include in conversion. have buffer may partially filled garbage. know start of possible valid data , end of possible valid data. don't want depend on whitespace end conversion, want explicit length of "field" because may not /0 terminated.
if sure garbage doesn't start digits can use atoi
/strtol
/istringstream
- automatically stop when see garbage. otherwise, use substr
method extract exact substring need:
std::string maycontaingarbage="alcak123456amaclmò"; std::string onlythedigits=maycontaingarbage.substr(5, 6); // parse onlythedigits prefer
Comments
Post a Comment