-6

I have a file with data in a certain layout. Each line contains an IP address which I have to extract it and compare it in to something else in C++.

One line looks like this:

t 0.00057 /NodeList/0/DeviceList/0/$ns3::WifiNetDevice/Phy/State/Tx ns3::WifiMacHeader (DATA ToDS=0, FromDS=0, MoreFrag=0, Retry=0, MoreData=0 Duration/ID=0usDA=ff:ff:ff:ff:ff:ff, SA=00:00:00:00:00:01, BSSID=00:00:00:00:00:01, FragNumber=0, SeqNumber=0) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 0 protocol 17 offset (bytes) 0 flags [none] length: 228 10.1.1.1 > 10.1.1.255) ns3::UdpHeader (length: 208 49153 > 80) Payload (size=200) ns3::WifiMacTrailer ()

The IP address which I have to extract is 10.1.1.1.

There are 10,000 lines like this in the file.

t 0.00057 /NodeList/0/DeviceList/0/$ns3::WifiNetDevice/Phy/State/Tx ns3::WifiMacHeader (DATA ToDS=0, FromDS=0, MoreFrag=0, Retry=0, MoreData=0 Duration/ID=0usDA=ff:ff:ff:ff:ff:ff, SA=00:00:00:00:00:01, BSSID=00:00:00:00:00:01, FragNumber=0, SeqNumber=0) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 0 protocol 17 offset (bytes) 0 flags [none] length: 228 10.1.1.1 > 10.1.1.255) ns3::UdpHeader (length: 208 49153 > 80) Payload (size=200) ns3::WifiMacTrailer () r 0.00287433 /NodeList/2/DeviceList/0/$ns3::WifiNetDevice/Phy/State/RxOk ns3::WifiMacHeader (DATA ToDS=0, FromDS=0, MoreFrag=0, Retry=0, MoreData=0 Duration/ID=0usDA=ff:ff:ff:ff:ff:ff, SA=00:00:00:00:00:01, BSSID=00:00:00:00:00:01, FragNumber=0, SeqNumber=0) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 0 protocol 17 offset (bytes) 0 flags [none] length: 228 10.1.1.1 > 10.1.1.255) ns3::UdpHeader (length: 208 49153 > 80) Payload (size=200) ns3::WifiMacTrailer ()

How can I get these IPs? Can anyone help me with the code?

TartanLlama
  • 63,752
  • 13
  • 157
  • 193

1 Answers1

0

Typically, you would open then file in read mode and read each line. It appears that each line is in certain fixed pattern & format. You can search for a sub-string inside each line and extract the IP address from it. For example: you can search for "[none] length:" and read the IP address string following it (Like by looking for the next blank space & pick sub string between those indexes).

If you are using MFC, then you can use CStdioFile::ReadString to read each line. If you are not using MFC, you can refer to this topic for reading a file line by line.

Community
  • 1
  • 1
Gautam Jain
  • 6,789
  • 10
  • 48
  • 67