Solved

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

Posted on 2004-09-18
3
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 12093924
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
ID: 12094082
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
ID: 12094170
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

705 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