tags:

views:

73

answers:

2

The text files are in this format :

No.     Time        Source                Destination           Protocol Info
      1 0.000000    192.168.1.77          203.253.16.41         DNS      Standard query A ksn1.kaspersky-labs.com

Frame 1: 83 bytes on wire (664 bits), 83 bytes captured (664 bits)
Ethernet II, Src: Giga-Byt_58:d7:ce (00:1f:d0:58:d7:ce), Dst: D-Link_c4:eb:4a (00:1b:11:c4:eb:4a)
Internet Protocol, Src: 192.168.1.77 (192.168.1.77), Dst: 203.253.16.41 (203.253.16.41)
User Datagram Protocol, Src Port: discp-server (2602), Dst Port: domain (53)
Domain Name System (query)

I would like a coding in C++ on how to read only the 192.168.1.77 and 203.253.16.41. And then holds the 2 IP address values and output it in another new .txt files.

I am developing this on Mac OS X 10.5.8 using Xcode...

As for now, I'm able to read the first line in 'iplist.txt' and write it to a new file named 'output.txt'. I need help on how to read the second line online using getline()...

Here's the part that i manage to do...

ifstream inFile("iplist.txt");

        getline(inFile,x);

        ofstream outFile ("output.txt");
            if (outFile.is_open())
            {
                outFile << x;
                outFile.close();
            }
+1  A: 

Read a line and discard it. Read the next line and break it into fields, keeping the third and fourth. Read the next 6 lines and discard them to get ready for the next record.

Jerry Coffin
I'm sorry but I'm quite new with C++. It's for my uni project. A code would pretty much help me...
Kunta
@Kunta: To read a line, you'd typically use `std::readline`. To read fields, you'd put the line into an `std::istringstream`, and then use `line >> field1 >> field2 >> field3 >> field4;`. For simplicity, you can define all the fields as `std::string`s. Just to be clear: while I'm happy to give advice about how to write the code, I'm *not* going to write the code for you.
Jerry Coffin
Thank you. I am trying to write the code now and will ask if there's any error...
Kunta
A: 

Come on. Who uses C++ for that? It is a one-line awk command:

awk '/^[ \t]/ { printf("%s\t%s\n", $3, $4); }' file.txt
Vlad Lazarenko
If you're going to use awk, you can make it a lot simpler still. If I'm not mistaken, something like: `"/^ [0-9]/ { print $3 $4; }"` should do the job.
Jerry Coffin
@Jerry: that will try to match the whole line... so there will be no matches. I bet awk alone can do it, but I don't know it enough to write a complex scripts :)
Vlad Lazarenko
I'm using C++ because later I'm going to visualize the IP address into maps using OpenGL...
Kunta
So pre-process this file with the shell script and feed your C++ application with normalized data so it doesn't have to parse anything.
Vlad Lazarenko
@Vlad: Not so. In fact, given that it's the only line starting with whitespace, you can simplify it even more: `awk "/^[ \t]/ { printf(\"%s\t%s\n\", $3, $4; }" addresses.txt` Doing a quick test, it looks like it's actually using a tab, so the previous one won't work (though the fix is trivial).
Jerry Coffin
@Jerry: That will work, I've replaced my ugly stuff with this awk-only solution.
Vlad Lazarenko
@Vlad: Why not do it in C++ like so: `system("awk '/^[ \t]/ { printf("%s\t%s\n", $3, $4); }' file.txt");`?
thyrgle
how do i use awk in C++??
Kunta
@thyrgle Because you cannot capture the output of `system`. It returns exit code. In order to capture the output, you have to fork the process, dupe descriptors, exec the bash with `-c` and command. It is a long story and I find combining scripts with `C++` easier than writing all in `C++`. Not to mention that `readline` plus a couple of `find_first_of` on `std::string` is much easier. But, you know, I don't think this is a place where people write code for you.
Vlad Lazarenko