🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #14362

Re: strtol question.

#14362
From: MWP
Date: Mon, 18 Aug 2003 14:46
61 lines
2810 bytes
It seems like this is a decent way of checking:

v = strtol(value, &end, 0 );
if((end-value) != strlen(value))
   // not a valid number


>"Ed Mulroy [TeamB]" <ed@mulroyREMOVETHESECHARS.org> wrote:
>
>>The documentation states that 0 is the return value from strtol when there
>>is an error.
>
>I think the OP realised that: "if the value is actually "0" or if its an
>invalid number?"
>
>>If you look in the source code for strtol you will be able to verify that it
>>also sets the errno variable.
>
>Sadly, although the source code indicates that it does, it doesn't work
>for me either (BCB5 + SP):
>
>  #include <cerrno>
>  #include <iostream>
>
>  int main()
>  {
>    char* endptr = 0;
>    char const* data = "***";
>    long res = strtol(data, &endptr, 10);
>    int i = std::errno;
>    std::cout << res << " " << i << " " << ERANGE;
>  }
>
>  C:\>test
>  0 0 34
>  C:\>
>
>(Temporary used just in case std::cout::operator<<() decided to muck
>around with the value of errno.)
>
>As far as my quick scan indicates, the only circumstance that causes
>errno to be set is an actual overflow, and the return value is unlikely
>to be zero in that case. This is as per the C Standard, and the client
>does need to sanity check the input string, probably scanning from the
>input value to endptr to see if there were any digits in the range,
>something like this:
>
>  if (res == 0)
>  {
>    bool is_valid_number = false;
>    for (char const* p = data; p != endptr && !is_valid_number; ++p)
>      if (std::isdigit(*ptr))
>        is_valid_number = true;
>  }
>
>(Not tested)
>
>Alan Bellingham
>-- 
>alanb@episys.com
Technical Details
Message-ID: <3f40e6c3$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!news-out.visi.com!petbe.visi.com!newshosting.com!news-xfer2.atl.newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3f40b30b$1@newsgroups.borland.com> <3f40c083$1@newsgroups.borland.com> <eqi1kvk5265bh6ocgr2p8i934dt2einqvd@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,ecc2b4647792de18
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-08-18 08:14:31 PST
From: "MWP" <mwp@overclockers.com.au>
Sender: "MWP" <mwp@overclockers.com.au>
Reply-To: "MWP" <mwp@overclockers.com.au>
Subject: Re: strtol question.
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 203.16.214.242 203.16.214.242    
X-Forwarded-For: unknown, 150.101.65.127
References: <3f40b30b$1@newsgroups.borland.com> <3f40c083$1@newsgroups.borland.com> <eqi1kvk5265bh6ocgr2p8i934dt2einqvd@4ax.com>
NNTP-Posting-Host: 207.105.83.65
Message-ID: <3f40e6c3$1@newsgroups.borland.com>
Date: 18 Aug 2003 07:46:27 -0700
X-Trace: newsgroups.borland.com 1061217987 207.105.83.65 (18 Aug 2003 07:46:27 -0700)
Lines: 62
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!news-out.visi.com!petbe.visi.com!newshosting.com!news-xfer2.atl.newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail