Thread: checking if a char * is an integer

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    10

    checking if a char * is an integer

    I have a line being read from the command line that contains 4 strings. The first is the command name, the second is supposed to be an integer value, the 3rd is the path to the source file, and the 4th is the path to the destination file. All of these arguments are being read in and stored in char * variables. The 2nd argument on the command line is the integer value. This is read in and stored as a char *, but is then converted into an integer by use of the atoi function. The problem is that I need to check if this argument is a valid positive integer before converting it to an actual integer. For example...

    commandname 55 source destination ... "55" is valid so the command can be executed

    commandname somenum source destination ... "somenum" is not a valid integer so the command can't be executed

    ... My question is, how can check if a char * is a positive integer before using the atoi function? I tried looking up some functions related to isalnum(), but none of those seemed to be what I was looking for.

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    strtol() can validate and convert all in one
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    10
    if i was to pass the string "39num" to strtol(), would that mean that it stops on the 'n' and returns 39 as the long int? or does it stop on the 'n' and return as a failure?

    possible scenarios:
    39 ... valid
    39num ... invalid
    num39 ... invalid
    num ... invalid

    from reading the man page on strtol, i am figuring that after calling strtol(), if *nptr != NULL and **endptr == NULL then the long int returned is the valid number after the conversion. but if those 2 cases aren't true, then no matter what the returned long int is, the conversion was not succesful based on the string being an invalid number. is this what would be happening in the case of "39num"?


    is it worth doing this the long way?
    parse the string char by char until " " or NULL is found, check if each char is a digit, if all chars are digits then concatenate them into a new string and then convert that string using the atoi function

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you apply the right checks (examine the value in errno, and examine what *endptr points to), you can achieve what you want to
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is it worth doing this the long way?
    Not unless you need specialized checking that the standard library doesn't offer. strtol is your best option if you want to make a conversion and have good error checking/validation as well.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    17
    If you got the second string which is to be converted in
    *string you could try this:

    Code:
    #include <string.h>
    
    char str2[] = "1234567890";
    
    int cnt = strspn ( string, str2 );
    
    if(cnt < strlen(string))
    {
    	Error()
    }else
    {
    	Convert()
    }
    strspn returns the first character in string, which is not in str2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03:12 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM