Re: [CODE] Char string manipulation

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 09/05/01


On Wed, 5 Sep 2001, Ragsdale, Jason D (Jason) wrote:

[First thing first: Please read the list FAQ concerning the list rules.
Specifically, this question is not CircleMUD--but C--related.  If you
use UNIX, you can always try using the apropos command with keywords to
find man pages for something you want.  For instance 'apropos format'
finds scanf(3) and 'man 3 scanf' gives a decent overview of how to use
it.  That said...]

> "0       Testing      0       0       0       Something"

The sscanf() function can do this for you.  It's sort of the inverse of
sprintf():

  int i[4];
  char buf1[32];
  char buf2[32];

  if (sscanf(str, "%d %32s %d %d %d %32s",
             i+0, buf1, i+1, i+2, i+3, buf2) != 6) {
    /*
     * You got an error: the string 'str' didn't have the six fields.
     * Do something to report it and recover in some way.
     */
  }

  /*
   * At this point i[0]..i[3] have the four integers, buf1 has the first
   * string, and buf2 has the second string.  Do something with it.
   */

You do not need to use more than one space between each of the fields in
the sscanf() format, regardless of how many spaces are in the input
string.  For instance, the above code parses:

  0 Foobar 1 2 3 Foobaz
  0  Foobar  1  2  3  Foobaz
  0  Foobar 1       2  3                   Foobaz

all in the same manner.


-dak

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/06/01 PST