delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/09/23/22:15:17

From: "bowman" <bowman AT montana DOT com>
Newsgroups: comp.os.msdos.djgpp
References: <39CABE68 DOT D5AA584B AT netzero DOT net> <969595461 DOT 718378 AT shelley DOT paradise DOT net DOT nz> <i35z5.4837$8X6 DOT 4960 AT newsfeed DOT slurp DOT net> <39CCF175 DOT 275438D7 AT netzero DOT net>
Subject: Re: Extract a value from a string?
Lines: 49
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2014.211
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
Message-ID: <Zcdz5.7472$8X6.7009@newsfeed.slurp.net>
Date: Sat, 23 Sep 2000 20:05:30 -0600
NNTP-Posting-Host: 208.26.212.185
X-Trace: newsfeed.slurp.net 969760697 208.26.212.185 (Sat, 23 Sep 2000 20:58:17 CDT)
NNTP-Posting-Date: Sat, 23 Sep 2000 20:58:17 CDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Kenneth A. Strom <therelic AT netzero DOT net> wrote in message
news:39CCF175 DOT 275438D7 AT netzero DOT net...
> Would it be better to strstr for = and then extract from the position
> of  = + 1 into another string and then atoi?  Does atoi handle
> whitespace?

there are several ways to do it. you might want to play with this
quick and dirty little example:

-------------   snip  -------------

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int main()
{
  char *ini[] =  { "Capture = 1234",
                   "Capture = 0",
                   "Capture = fubar",
                    NULL};
  char *ptr;
  int num;
  int  i;

  for (i=0; ini[i]; i++) {
    printf("the string is \"%s\"\n", ini[i]);
    if (sscanf(ini[i], "%*s = %d", &num) == 1) {
      printf("sscanf num = %d\n", num);
    }
    else {
      printf("sscanf failed\n");
    }

    ptr = strchr(ini[i], '=');
    if (ptr) {
      num = atoi(ptr+1);
      printf("atoi num = %d\n", num);
    }
    printf("\n");
  }
  return 0;
}





- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019