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.
Transforming a stream of text into some other form of information is called "parsing".
It is very common in computer programs for a wide range of purposes.
For example, when the user types a reply to any prompt in your program.
johnsfine said it well, I was going to jump on their coattails and elaborate a bit; however I'll address your question about "why".
The answer is you are not required to use parsing to write program in C. And you are not required to write the equivalent of a parser when you write a program in C. It's more like a sort algorithm, if you need to do that, you do it, not because there's some purpose why every C program necessitates the need for a sort algorithm.
You can write or use a parser, as someone has done in the example you've cited and it is a very common programming action to perform when you're converting information from one form to another.
Be aware that verbiage use by a programmer does not guarantee that they are doing exactly what they used for names. In this case I do agree that they are parsing a string; however it seems odd that the name of the string is "binaryReply" which would imply to me that the data potentially could be non-printable ASCII. Further where they say //Skip 0, you have to wonder if that's 0x30 which is ASCII printable "0" or if that's 0x00 which is the NULL string terminator. That would cause some problems, so a guess is that they're skipping a field which they know to be there whatever the content of it is. The strtok() function will just return the next string found after a comma.
The programmer is not verifying the return from strtok(), it could be NULL, and therefore a lot of their following actions could end up becoming segment violations. The string is very customized. How many strings would be exactly: "Hello,<something>,00:00:00:00:00:00\0" as an example? Because that's approximately what it appears they're parsing specifically for. And if there's a format screwup anywhere in there, the program goes kaboom!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.