-1

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?

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
赵浩翔
  • 643
  • 1
  • 5
  • 13

1 Answers1

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 ints (or unsigned, etc.) instead of strings.

Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111