exactly, I want to write a program to transfer IP address to its binary format like 202.117.1.20.So is there a way that can make input separated by dot so that I can read 202 as int but not string? Modify: why the input stream separatd by space, can I change it to dot,or any character I like?
Asked
Active
Viewed 726 times
-1
-
1You might be interested in [`inet_addr`](http://beej.us/guide/bgnet/output/html/multipage/inet_ntoaman.html) or a related function. – Greg Hewgill Apr 22 '15 at 01:24
-
1http://stackoverflow.com/questions/12961189/separating-input-on-forward-slash-in-c/12961404#12961404 – Martin Beckett Apr 22 '15 at 01:29
1 Answers
3
The obvious possibility would be something like:
unsigned o1, o2, o3, o4;
char dot1, dot2, dot3;
infile >> o1 >> dot1 >> o2 >> dot2 >> o3 >> dot3 >> o4;
assert(dot1=='.' && dot2=='.' && dot3=='.');
assert(o1 < 256 && o2 < 256 && o3 < 256 && o4 < 256);
If you really don't want to explicitly read the .
characters, you can create a ctype facet that classifies .
as whitespace, then create a locale using that facet, and imbue the stream with that locale.
If you need to write a lot of code that ignores .
in the input, this might be worthwhile. I've posted such a facet in another answer. Here you'd use that, but read int
s (or unsigned
, etc.) instead of strings.

Community
- 1
- 1

Jerry Coffin
- 476,176
- 80
- 629
- 1,111