![]() |
|
|
|||||||
![]() |
C Programming - Re: How can I validate and store IP address that user enter from command line? |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Hi,
In order to get the inputs from command line, you can us gets () API. After you get the number, use strchr () to see if the input value contains "-". if it is, then use strstr to get the substring (range value) starting from "-". In this way, you will get two diff strings that will be two IP address... In the strings you've got, again use strchr to see that there's atleast one dot (.). If neither strings contains dot, print invalid string and exit.. otherwise. determine which is the last byte in both the string.. Calculate the difference and allocate array and start STORINg. (Abby) wrote in message news:<. com>... > My program will need to get ip address from command line. User can > enter a single IP or multiple IPs(enter in range). > > For examples, > > c:\>programname 192.168.0.1 --> user enter a single ip > > c:\>programname 192.168.0.1 - 192.168.0.100 --> user enter a range of > ip > > My question is: > > 1. How to get a range IP addresses and store them into an array? I'm > not sure if my example is the easiest way to get it or there're other > ways easier than this one. I need the program to automatically fill in > an array from ip 192.168.0.1 until 192.168.0.100. How can I do that? > > 2. How can I validate if user has entered valid IP address? Any > function or easy way to do it? > > Thank you so much. > > Abb. sumit.sharma@wipro.com |
|
|
|
|
#2 |
|
Posts: n/a
|
wrote:
[ Do not top-post. Learn to snip. ] > (Abby) wrote in message news:<. com>... > > My program will need to get ip address from command line. User can > > enter a single IP or multiple IPs(enter in range). > > > > For examples, > > > > c:\>programname 192.168.0.1 --> user enter a single ip > > > > c:\>programname 192.168.0.1 - 192.168.0.100 --> user enter a range of > > ip > > In order to get the inputs from command line, you can us gets () API. NO. Not ever. gets() is the single most dangerous function in C, because it cannot be used safely except with the use of outside appliances, such as chains and straitjackets. There is no way to prevent it from reading too much input into its buffer, because there is no way for it to know how large that buffer is. NEVER use gets(). Use fgets() instead. Besides, neither gets() nor fgets() reads from the command line. They read from standard input, which is a different matter entirely. To read from the command line, use the arguments to main(): int main(int argc, char **argv) > After you get the number, use strchr () to see if the input value contains > "-". if it is, then use strstr to get the substring (range value) starting > from "-". > > In this way, you will get two diff strings that will be two IP > address... You hope. Make sure that they actually _are_ IP addresses > In the strings you've got, again use strchr to see that there's atleast > one dot (.). If neither strings contains dot, print invalid string and exit.. That's not enough. It's a start, but it's not enough. > > 1. How to get a range IP addresses and store them into an array? I'm > > not sure if my example is the easiest way to get it or there're other > > ways easier than this one. I need the program to automatically fill in > > an array from ip 192.168.0.1 until 192.168.0.100. How can I do that? Question: how do you want to handle 192.168.1.0 - 192.168.100.0? Subnet masks of 255.255.0.128 are valid, AFAICT. Unwise, of course, but valid. > > 2. How can I validate if user has entered valid IP address? Any > > function or easy way to do it? Do it by hand, as demonstrated by earlier replies. Note: use strtoul(), not atoi(), unless you do some pre-checking. You do not want to invoke undefined behaviour if some joker types "yourprogram 34278459780489034". Richard Richard Bos |
|
|
|
#3 |
|
Posts: n/a
|
wrote:
> Hi, > > In order to get the inputs from command line, you can us gets () API. You are kidding?! Never, never use gets(), unless you are Dan Pop and know which platform you are using. Also the OP obviously meant to get arguments passed to his program instead of reading from the standard input stream. There we want to use the parameters from one of mains legal defintions/declarations: int main(int argc, char **argv) where argv will hold pointers to the strings representing the arguments given at invocation of the program. > > After you get the number, use strchr () to see if the input value contains > "-". if it is, then use strstr to get the substring (range value) starting > from "-". If the arguments are read from argv there will be no need to do this. > > In this way, you will get two diff strings that will be two IP > address... argv[1] and argv[3] should be the two strings. > > In the strings you've got, again use strchr to see that there's atleast > one dot (.). If neither strings contains dot, print invalid string and exit.. Verifying that a string is a ip-address will probably need mor than this. > > otherwise. determine which is the last byte in both the string.. > Calculate the difference and allocate array and start STORINg. > > > (Abby) wrote in message news:<. com>... >> My program will need to get ip address from command line. User can >> enter a single IP or multiple IPs(enter in range). >> >> For examples, >> >> c:\>programname 192.168.0.1 --> user enter a single ip >> >> c:\>programname 192.168.0.1 - 192.168.0.100 --> user enter a range of >> ip >> >> My question is: >> >> 1. How to get a range IP addresses and store them into an array? I'm >> not sure if my example is the easiest way to get it or there're other >> ways easier than this one. I need the program to automatically fill in >> an array from ip 192.168.0.1 until 192.168.0.100. How can I do that? >> >> 2. How can I validate if user has entered valid IP address? Any >> function or easy way to do it? >> >> Thank you so much. >> >> Abb. -- Z () "LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond Zoran Cutura |
|
|
|
#4 |
|
Posts: n/a
|
(Richard Bos) writes:
> wrote: > > > In order to get the inputs from command line, you can us gets () API. > > NO. Not ever. gets() is the single most dangerous function in C, because > it cannot be used safely except with the use of outside appliances, such > as chains and straitjackets. Think chains and straitjackets would be enough to make this usage safe on the DS9000? ....straightjacket for the programmer, maybe -Micah Micah Cowan |
|