473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

formating an stdin input

momotaro
357 100+
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
Aug 3 '07 #1
8 1325
JosAH
11,448 Expert 8TB
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
Aug 3 '07 #2
momotaro
357 100+
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!
Aug 3 '07 #3
momotaro
357 100+
am thinking about a function that keps reading my string findind the positions of the dot's then returning them what do you think.?
Aug 3 '07 #4
JosAH
11,448 Expert 8TB
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
Aug 3 '07 #5
momotaro
357 100+
no we can't but the dot idea solve 90% of the problem still left with how to turn the red digits into integers!
Aug 3 '07 #6
momotaro
357 100+
could I use this function:
Expand|Select|Wrap|Line Numbers
  1. int atoi(const char *s);
  2.  
Aug 3 '07 #7
JosAH
11,448 Expert 8TB
could I use this function:
Expand|Select|Wrap|Line Numbers
  1. int atoi(const char *s);
  2.  
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
Aug 3 '07 #8
momotaro
357 100+
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:
Expand|Select|Wrap|Line Numbers
  1. for(i = 0; i < 4; i++)
  2.    {
  3.     if(i == 0)
  4.     {
  5.      for(j = i; j < pos[i]; j++)
  6.      {
  7.       if(isdigit(IP[j]))
  8.        xx[j] = IP[j];
  9.      }
  10.      printf("xxx part\n");
  11.         printf("%s\n", xx);
  12.     }
  13.     else if(i == 1)
  14.     {
  15.      for(j = pos[i-1]+1; j < pos[i]; j++)
  16.      {
  17.       if(isdigit(IP[j]))
  18.       {
  19.        yy[y] = IP[j];
  20.        y++;   //****
  21.       }
  22.      }
  23.      printf("yyy part\n");
  24.      printf("%s\n", yy);
  25.     }
  26.     else if(i == 2)
  27.     {
  28.      for(j = pos[i-1]; j < pos[i]; j++)
  29.      {
  30.       if(isdigit(IP[j]))
  31.       {
  32.        zz[z] = IP[j];
  33.        z++; //****
  34.       }
  35.      }
  36.      printf("zzz part\n");
  37.      printf("%s\n", zz);
  38.     }
  39.     else if(i == 3)
  40.     {
  41.      for(j = pos[i-1]+1; j < 15; j++)
  42.      {
  43.       if(isdigit(IP[j]))
  44.       {
  45.        mm[m] = IP[j];
  46.        m++; // ******
  47.       }
  48.      }
  49.      printf("mmm part\n");
  50.      printf("%s\n", mm);
  51.     }
  52.    }
  53.  
Aug 6 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
3
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. ...
2
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?
23
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...
4
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...
6
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...
11
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 /*...
9
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...
2
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:...
5
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 ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
0
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...
1
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...
1
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....
0
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
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.