c++ - Error with strtok Function -
in code unhandled expression error
when use parse function.
in popstack
function right way delete last element of vector.
error is:
unhandled exception @ 0x0f463b50 (msvcr100d.dll) in boost_test.exe: 0xc0000005: access violation writing location 0x00345d49.
class stack { public: stack() {globalindex=0; }; std::vector<char*> v; int globalindex; void addstack(char* txt); void parse(); void popstack(); void printstack(); }; void stack::parse() { char* tok; tok = strtok(v[globalindex-1], ";"); while(tok!=null) { cout<<"\nthe time value = "<<tok<<endl; tok = strtok(null, " "); } } void stack::addstack(char* txt) { v.push_back(txt); globalindex++; } void stack::popstack() { v.pop_back(); globalindex--; } void stack::printstack() { std::cout<<v[globalindex-1]<<endl; } int _tmain(int argc, _tchar* argv[]) { int i; stack s; s.addstack("aaa;1.2"); s.addstack("bbb;1.7;"); s.addstack("ccc;2.2"); s.parse(); // gives unhandled expression error s.popstack(); s.printstack(); return 0; }
the end of token found, in case ';', replaced 0.
this write operation done on string literal pass:
s.addstack("aaa;1.2");
but literal not writable, 'const char *', hence access violation.
Comments
Post a Comment