Solved

Tokenizing a string (since strtok() appears to be deprecated)

Posted on 2004-09-18
3
420 Views
Last Modified: 2008-02-01
Okay, first off, this question *is* related to a homework assignment, however it's not the main thrust of it -- the assignment is to write a rudimentary analogue of gdb for a toy VM for an operating systems course.   That being said, if anyone doesn't want to answer on the basis of the preceding, feel free.

Anyway, as part of the debugger, I'm needing to parse commands entered by the user.  What I need is to take a string and get two things out of it.. essentially argc and argv[].  So, since every manpage on the subject says "Don't use strtok!!!" what are the alternatives, and how should I go about getting all of this info?  The text is going to be a series of integer values separated by spaces.  Anything else should allow me to spit out an error.

I just want to reiterate that this is *not* the assignment, this is a small part of it, and the rest is the hard part -- which I'm doing myself.  Please don't chew me out over this :)
0
Comment
Question by:offline314159
  • 2
3 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
Comment Utility
If you have to parse a string that contains many integers separated by spaces, then sscanf() appears to be enough.
Even if you have different arguments for different command, then you can analize command first and the do sscanf() according to command syntax.
0
 

Author Comment

by:offline314159
Comment Utility
Sounds promising...

However, there's the potential of arbitrary numbers of arguments.  Is there a graceful way of handling this?  I suppose there's always an array of int* that I can fill as I go, keeping count...

I would prefer an easier method, though :)
0
 

Author Comment

by:offline314159
Comment Utility
Oh well. I'm just limiting the number of arguments to something manageable, and I'll hope that it's good enough.  What are the odds that they're going to throw more than 10 addresses at a set breakpoint instruction anyway ?  (famous last words, I know...)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now