c++ - How to handle user specified string formatting?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
c++ - How to handle user specified string formatting?
I want a user to be able to give me a string:
foo.%04d.bar
such that when event '100' comes around, that string turns into:
foo.0100.bar
... and when event 597 comes around, we get
foo.0597.bar
... and so on
Is there a neat way of doing this without parsing the string char by char to find a '%', a 'd' and then figuring what's in between them? I seem to remember something like this existing, but I can't seem to remember how its done.
You should still check to make sure that you have enough arguments to match the number of %s so you don't overrun the stack. Also, you need to parse out %n because those can be used to hack your program, and maybe %m since it won't take an argument. http://en.wikipedia.org/wiki/Format_...ulnerabilities
no offense, but where does the user give me formatting instructions and how does this differ from printf? For the record, I get a single string from the user & am trying to avoid parsing that string by hand. Maybe I'm just missing something in your example.
Quote:
Originally Posted by ta0kira
You should still check to make sure that you have enough arguments to match the number of %s so you don't overrun the stack. Also, you need to parse out %n because those can be used to hack your program, and maybe %m since it won't take an argument. http://en.wikipedia.org/wiki/Format_...ulnerabilities
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.