C ++ sscanf equivalent?

I use sscanf , and I think I feel too comfortable. Apparently this is also deprecated, and I should use sscanf_s , which, as far as I know, is not standard. So I was wondering if STL makes an idiomatic C ++ replacement the same?

thanks

I do:

  sscanf(it->second->c_str(),"%d %f %f %f %f %f %f %d \" %[^\"] \" \" %[^\"]", &level, &x, &y, &angle, &length, &minAngle, &maxAngle, &relative, name,parentName); 
+6
source share
5 answers

Formatting is not easy, but check stringstream . See also istringstream and ostringstream for formatting input and output buffers.

+11
source

In C ++, the final parser is Boost.Qi

 #include <boost/spirit/include/qi.hpp> #include <string> namespace qi = boost::spirit::qi; int main() { int level, relative; float x, y, angle, length, minAngle, maxAngle; std::string name, parentName; std::string input = "20 1.3 3.7 1.234 100.0 0.0 3.14 2 \"Foo\" \"Bar\""; std::string::iterator begin = input.begin(); std::string::iterator end = input.end(); using qi::int_; using qi::float_; using qi::char_; using qi::lit; using qi::ascii::space; qi::phrase_parse(begin, end, int_ >> float_ >> float_ >> float_ >> float_ >> float_ >> float_ >> int_ >> lit("\"") >> *(~char_('"')) >> lit("\"") >> lit("\"") >> *(~char_('"')) >> lit("\""), space, level, x, y, angle, length, minAngle, maxAngle, relative, name, parentName); } 
+9
source

You can try using stringstream . It is much more powerful than sscanf and serves the purpose.

+1
source

I believe string streams are what you are looking for.

eg:

 stringstream tokenizer; string str("42"); int number; tokenizer << string; tokenizer >> number; 
+1
source

If you use a compiler with sufficient C ++ 0x support, it’s easy to write a function that is type safe for scanf()-style ... read the printf() example at http://en.wikipedia.org/wiki/C%2B%2B0x , to get you started ...

+1
source

Source: https://habr.com/ru/post/888869/


All Articles