the idea that am trying to impliment is to read an ip adress intred by the user in the format 'xxx.yyy.zzz.mmm' , detect all possible errors then cut it into xxx, yyy, zzz, mmm, each one must be put in a member of a structure.
am confused i could play with getc, getchar...
but plz if u have a better idea and may be easier plz tell me
8 1325
the idea that am trying to impliment is to read an ip adress intred by the user in the format 'xxx.yyy.zzz.mmm' , detect all possible errors then cut it into xxx, yyy, zzz, mmm, each one must be put in a member of a structure.
am confused i could play with getc, getchar...
but plz if u have a better idea and may be easier plz tell me
For a bit of thorough checking read your string (a potential IP/4 address) and:
1) check if the string length == 15
2) at positions 3, 7 and 11 should be dots.
3) at all other positions should be digits.
4) the three numbers should be in the range [0,255]
Making an int number out of three consecutive character positions containing
the ASCII representation of digits is easy.
scanf("%d" ...) is a nono here because it accepts leading spaces which you
don't want. The function 'isdigit()' in file ctype.h is your friend.
kind regards,
Jos
but the string is not alway 15!
for example: 34.54.234.1 is a valide IP adress yet it's not a 15 string lenght!
am thinking about a function that keps reading my string findind the positions of the dot's then returning them what do you think.?
but the string is not alway 15!
for example: 34.54.234.1 is a valide IP adress yet it's not a 15 string lenght!
Ah, ok, the rules have to be a bit more relaxed then; are you allowed to use
regular expression? as in \d+\.\d+\.\d+\.\d+
That'd be the Java way of doing it; the Boost library has a regular expression
parser and matcher too.
If you can't use those follow that pattern 'manually':
1) read one or more digits
2) read a dot.
3) read one or more digits
4) repeat steps 2) and 3) three times.
If any of the steps fail or if the number read wasn't in the [0,255] interval,
the pattern wasn't a valid IP/4 address.
kind regards,
Jos
no we can't but the dot idea solve 90% of the problem still left with how to turn the red digits into integers!
could I use this function:
could I use this function:
I think you could if you take a few precautions:
1) the atoi function stops when it read a non-digit; you have to find where that non-digit is;
2) atoi() doesn't care about the range of the int; you have to take care of that yourself.
kind regards,
Jos
the have been modified(*)
pre: - array pos[3] containing the dots' position in the IP adress
- array IP[15] containing the IP string
post:- return 4 arrays xx[3], yy[3], zz[3], tt[3] containing the IP/4 digits
problem(remaining from 1st version): - unexpected result while checking using printf
example: - instead having "xx[3] = 123" am having 123:)
- third part of the IP 123 and some bizard char instead of 123
questions: - please help(if there is any other comment...)
CODE: -
for(i = 0; i < 4; i++)
-
{
-
if(i == 0)
-
{
-
for(j = i; j < pos[i]; j++)
-
{
-
if(isdigit(IP[j]))
-
xx[j] = IP[j];
-
}
-
printf("xxx part\n");
-
printf("%s\n", xx);
-
}
-
else if(i == 1)
-
{
-
for(j = pos[i-1]+1; j < pos[i]; j++)
-
{
-
if(isdigit(IP[j]))
-
{
-
yy[y] = IP[j];
-
y++; //****
-
}
-
}
-
printf("yyy part\n");
-
printf("%s\n", yy);
-
}
-
else if(i == 2)
-
{
-
for(j = pos[i-1]; j < pos[i]; j++)
-
{
-
if(isdigit(IP[j]))
-
{
-
zz[z] = IP[j];
-
z++; //****
-
}
-
}
-
printf("zzz part\n");
-
printf("%s\n", zz);
-
}
-
else if(i == 3)
-
{
-
for(j = pos[i-1]+1; j < 15; j++)
-
{
-
if(isdigit(IP[j]))
-
{
-
mm[m] = IP[j];
-
m++; // ******
-
}
-
}
-
printf("mmm part\n");
-
printf("%s\n", mm);
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Stelios G. Sfakianakis |
last post by:
Hello,
I am using php 4.2.2 in Red Hat 9.0 with apache 2.0.40 I try to build a
php script that accepts POST requests that contain multimedia data and
shoves them in a MySQL database. My problem is...
|
by: Mike Tammerman |
last post by:
Hi,
I want create a subprocess using Popen and pipe some input to it.
Although everything works perfectly while executing python in, it
doesn't work if I try with executables made by py2exe.
...
|
by: gc |
last post by:
I was having trouble getting fgets to read a string from stdin, it was
reading a '\n' already in the buffer.
Someone told me to rewind stdin before calling fgets. It works, but is
it permissible?
|
by: herrcho |
last post by:
What's the difference between STDIN and Keyboard buffer ?
when i get char through scanf, i type in some characters and press
enter,
then, where do the characters go ? to STDIN or Keyboard...
|
by: Johnathan Doe |
last post by:
I'm trying to find a way to reliably peek at stdin, and if anything's
waiting, flush stdin so that it clears the stream ready to wait for a
character.
The problem I have is that in an...
|
by: Charlie Zender |
last post by:
Hi,
I have a program which takes the output filename argument from stdin.
Once the program knows the output filename, it tries to open it.
If the output file exists, the program asks the user to...
|
by: Darklight |
last post by:
is this correct or incorrect i just read the post below before posting
this: In fflush(stdin), what happens to flushed data?
this program is taken from sams teach yourself c in 21 days
/*...
|
by: kernelxu |
last post by:
hi, everyone.
now, I'am confused on such a problem:
function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0)
can set the stadard input stream unbuffered.
however, why does my program...
|
by: Dmitry Anikin |
last post by:
I want to read stdin in chunks of fixed size until EOF
I want to be able (also) to supply data interactively in console
window and then to hit Ctrl+Z when finished
So what I do is:
while True:...
|
by: Luis Zarrabeitia |
last post by:
I have a problem with this piece of code:
====
import sys
for line in sys.stdin:
print "You said!", line
====
Namely, it seems that the stdin buffers the input, so there is no reply until ...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |