string validation for int and long (strisint, strislong)
From: jake1138 (cooljake_at_gmail.com)
Date: 02/25/05
- Next message: Old Wolf: "Re: identity theft"
- Previous message: Peetah_junkmail: "[OT] pdf version of C unleashed ?"
- Next in thread: Peter Nilsson: "Re: string validation for int and long (strisint, strislong)"
- Reply: Peter Nilsson: "Re: string validation for int and long (strisint, strislong)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 24 Feb 2005 15:53:10 -0800
Maybe this is a newbie thing and everyone already knows how to do this,
but I figured I'd post these functions anyway in case someone finds
them useful. I used Jack Klein's example (see link below) and made
functions out of it.
http://home.att.net/~jackklein/c/code/strtol.html
Here are functions that will validate a string to see if it represents
an integer or long, respectively:
/*
* checks to see if string is an integer
*
* returns 1 if true, 0 if false
*/
int strisint(const char *str, size_t size)
{
char *endptr;
long longint;
errno = 0;
longint = strtol(str, &endptr, 10);
if (errno == ERANGE || longint < INT_MIN || longint > INT_MAX
|| endptr == str || *endptr != '\0') {
return 0;
} else {
return 1;
}
}
/*
* checks to see if string is a long integer
*
* returns 1 if true, 0 if false
*/
int strislong(const char *str, size_t size)
{
char *endptr;
long longint;
errno = 0;
longint = strtol(str, &endptr, 10);
if (errno == ERANGE || endptr == str || *endptr != '\0') {
return 0;
} else {
return 1;
}
}
- Next message: Old Wolf: "Re: identity theft"
- Previous message: Peetah_junkmail: "[OT] pdf version of C unleashed ?"
- Next in thread: Peter Nilsson: "Re: string validation for int and long (strisint, strislong)"
- Reply: Peter Nilsson: "Re: string validation for int and long (strisint, strislong)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|