Read data from a file

hey all
I am trying to read & extract data from a trace file
I have created a program where I can count the number of received, sent, dropped pkts and then calculates the Received/Received+ Drop and it did compile successfully
I am now trying to count the number of lines that contain both the character r and the word AGT as well but when doing so I keep getting it as 0
Can u plz help me out.. I don't know what am I doing wrong ?
thanx in advance


[co
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <math.h>
#include <stdio.h>
using namespace std;

int main() {

float received = 0.0;
float agent = 0.0 ;
float send = 0.0;
float drop = 0.0;
char c;
int numlines = 0;
string n;
string line;

ifstream myfile ("IMPD.tr");
if(myfile.is_open()) {

while (getline(myfile,line)) {
numlines++;
istringstream input(line);
input >>c;
input >>n;

if((c == 'r' )&&(n =="AGT"))
received++;
else if (n == "AGT")
agent++;
else if(c == 's')
send++;
else if(c == 'D')
drop++;

}
cout << "n = " << n << ".\n";
cout << "received pkts = " << received << ".\n";
cout << "agent = " << agent << ".\n";
cout << "send pkts =" << send << ".\n";
cout <<"Dropped pkts =" << drop << ".\n";
float success ratio = (received/(received + drop))* 100;
float a = received + drop;
cout << a << ".\n";
float b = received;
cout << b << ".\n";
float m = b/a;
cout << m << ".\n";
cout << "The number of lines = " << numlines << ".\n";

myfile.close();

}
else cout << "Unable to open file";

return 0;

}
The olp I get
n = 150.000000000.
received pkts = 0.
agent = 0.
send pkts =133958.
Dropped pkts =7836.
7836.
0.
0.
The number of lines = 268264.
1
2
3
4
5
6
7
input >>c; // puts entire line into single char?
input >>n; // puts entire line into string

if((c == 'r' )&&
    (n =="AGT")) // this does not check if string CONTAINS "AGT". 
                 // It checks if string EQUALS "AGT". 
                 // "r - AGT - Hello" does not EQUAL "AGT". 
I need to be careful what I ask for here. It would be helpful if you could show an example of the contents of the file - but not the entire file, just a few lines would do, to show the typical contents of the lines that you are interested in.

The [output] text here [/output] tags may be useful in order to preserve spacing.
Thanx Guys for the quick reply ...ok how can I check if I found my word AGT
here's a snap shot of my trace file I am trying to extract data off
r 11.101753057 _9_ MAC --- 30 tcp 1060 [13a 9 3 800] ------- [3:2 9:2 32 9] [2 0] 1 0
s 11.101763057 _9_ MAC --- 0 ACK 38 [0 3 0 0]
r 11.101778057 _9_ AGT --- 30 tcp 1060 [13a 9 3 800] ------- [3:2 9:2 32 9] [2 0] 1 0
s 11.101778057 _9_ AGT --- 40 ack 40 [0 0 0 0] ------- [9:2 3:2 32 0] [2 0] 0 0
r 11.101778057 _9_ RTR --- 40 ack 40 [0 0 0 0] ------- [9:2 3:2 32 0] [2 0] 0 0
s 11.101778057 _9_ RTR --- 40 ack 60 [0 0 0 0] ------- [9:2 3:2 32 3] [2 0] 0 0
r 11.102067147 _3_ MAC --- 0 ACK 38 [0 3 0 0]
s 11.102257156 _2_ MAC --- 0 RTS 44 [62e 3 2 0]
r 11.102609191 _3_ MAC --- 0 RTS 44 [62e 3 2 0]
s 11.102619191 _3_ MAC --- 0 CTS 38 [4f4 2 0 0]

plzz help me out .. thanx
This might be useful (if you read each line to a std::string):
http://www.cplusplus.com/reference/string/string/find/
I just did a very quick beginning of a program.
1
2
3
4
5
6
7
8
9
10
11
    ifstream fin("trace.txt");
    string line;
    string a, b, c, d;

    while (getline(fin, line) )
    {
        istringstream ss(line);
        ss >> a >> b >> c >> d;
        if (a == "r" &&  d == "AGT")
            cout << line << endl;
    }

Output:
r 11.101778057 _9_ AGT  --- 30 tcp 1060 [13a 9 3 800] ------- [3:2 9:2 32 9] [2 0] 1 0<br>


I see that the later part of each line may vary, but I assumed that all lines start out with the same layout, and each is field separated by whitespace.
Last edited on
Thank u soo much for the link and the code it is quite helpful and I did try the code and it successfully compiled
and that was the o/p
d = IFQ.
received pkts = 19495.
agent = 0.
send pkts =133958.
Dropped pkts =7836.
The number of lines = 268264

I don't get what does d= IFQ mean

and can u plz correct me if I understood this codeline in a wrong way ss >> a >> b >> c >> d; This codeline is written to scan through the line and get to the word ''AGT'' but what if I want to count the lines that have the words tcp and received and AGT or the r and the time (which is the second value).
thanx in advance
This line ss >> a >> b >> c >> d; simply splits the line into individual words whenever it finds whitespace.

With a slight modification, I get this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    ifstream fin("trace.txt");
    string line;
    string a, b, c, d, e, f, g;

    while (getline(fin, line) )
    {
        istringstream ss(line);
        ss >> a >> b >> c >> d >> e >> f >> g;
        
        cout <<   "a: " << a << "  b: " << b << "  c: " << c << "  d: " << d
             << "  e: " << e << "  f: " << f << "  g: " << g << endl;
        if (a == "r" &&  d == "AGT")
            cout << line << endl;
    }

Output:
a: r  b: 11.101753057  c: _9_  d: MAC  e: ---  f: 30  g: tcp
a: s  b: 11.101763057  c: _9_  d: MAC  e: ---  f: 0  g: ACK
a: r  b: 11.101778057  c: _9_  d: AGT  e: ---  f: 30  g: tcp
r 11.101778057 _9_ AGT  --- 30 tcp 1060 [13a 9 3 800] ------- [3:2 9:2 32 9] [2 0] 1 0<br>
a: s  b: 11.101778057  c: _9_  d: AGT  e: ---  f: 40  g: ack
a: r  b: 11.101778057  c: _9_  d: RTR  e: ---  f: 40  g: ack
a: s  b: 11.101778057  c: _9_  d: RTR  e: ---  f: 40  g: ack
a: r  b: 11.102067147  c: _3_  d: MAC  e: ---  f: 0  g: ACK
a: s  b: 11.102257156  c: _2_  d: MAC  e: ---  f: 0  g: RTS
a: r  b: 11.102609191  c: _3_  d: MAC  e: ---  f: 0  g: RTS
a: s  b: 11.102619191  c: _3_  d: MAC  e: ---  f: 0  g: CTS

Here you can see that string g would be the one to check for "tcp".
It's just a matter of applying whatever tests or selection criteria you like to the appropriate variable.

Of course you might still find it useful to try string.find() but doing it this way allows you to home in directly on a particular column of data rather than searching the entire string.
Last edited on
Thank u soo much.. its quite helpful and useful, i have better understanding now and the code is working fine :)
hey all
i tried using the same code to print out and count some items in a similar trace file
r 3.507270487 _0_ MAC --- 3 ack 60 [13a 0 1 800] ------- [1:0 0:0 32 0] [0 0] 1 0
s 3.507280487 _0_ MAC --- 0 ACK 38 [0 1 0 0]
r 3.507295487 _0_ AGT --- 3 ack 60 [13a 0 1 800] ------- [1:0 0:0 32 0] [0 0] 1 0
s 3.507295487 _0_ AGT --- 4 tcp 1040 [0 0 0 0] ------- [0:0 1:0 32 0] [1 0] 0 0
r 3.507295487 _0_ RTR --- 4 tcp 1040 [0 0 0 0] ------- [0:0 1:0 32 0] [1 0] 0 0
s 3.507295487 _0_ RTR --- 4 tcp 1060 [0 0 0 0] ------- [0:0 1:0 32 1] [1 0] 0 0
s 3.507295487 _0_ AGT --- 5 tcp 1040 [0 0 0 0] ------- [0:0 1:0 32 0] [2 0] 0 0
r 3.507295487 _0_ RTR --- 5 tcp 1040 [0 0 0 0] ------- [0:0 1:0 32 0] [2 0] 0 0
s 3.507295487 _0_ RTR --- 5 tcp 1060 [0 0 0 0] ------- [0:0 1:0 32 1] [2 0] 0 0
r 3.507584536 _1_ MAC --- 0 ACK 38 [0 1 0 0]
s 3.507674487 _0_ MAC --- 0 RTS 44 [256e 1 0 0]
s 3.775336507 _0_ MAC --- 0 RTS 44 [256e 1 0 0]
s 6.033650740 _0_ MAC --- 335 tcp 1118 [13a 1 0 800] ------- [0:0 1:0 32 1] [176 0] 0 0

that's a snap shot of my code
ifstream myfile ("dees.tr");
if(myfile.is_open()) {
while (getline(myfile,line)) {
numlines++;
istringstream input(line);
input >> c >> b >> m >> d >> e >> f >> g >> h >> q >> i >> o >> j
>> k >> l >> y >> n >> p >> r;
cout << "c:" << c<< " b:" << b << " m:" << m << " g:" << g << " h:"
<< h << " q:" << q << " i:" << i << " o:" << o << " j:" << j << "k:"
<< k << " l:" << l << " y:" << y << " n:" << n << " p :" << p << " r:"
<< r << ".\n";
and that's the o/p
c:r b:3.507270487 m:_0_ g:ack h:60 q:[13a i:0 o:1 j:800]k:------- l:[1:0 y:0:0 n:32 p :0] r:[0.
c:s b:3.507280487 m:_0_ g:ACK h:38 q:[0 i:1 o:0 j:0]k:------- l:[1:0 y:0:0 n:32 p :0] r:[0.
c:r b:3.507295487 m:_0_ g:ack h:60 q:[13a i:0 o:1 j:800]k:------- l:[1:0 y:0:0 n:32 p :0] r:[0.
c:s b:3.507295487 m:_0_ g:tcp h:1040 q:[0 i:0 o:0 j:0]k:------- l:[0:0 y:1:0 n:32 p :0] r:[1.
c:r b:3.507295487 m:_0_ g:tcp h:1040 q:[0 i:0 o:0 j:0]k:------- l:[0:0 y:1:0 n:32 p :0] r:[1.
c:s b:3.507295487 m:_0_ g:tcp h:1060 q:[0 i:0 o:0 j:0]k:------- l:[0:0 y:1:0 n:32 p :1] r:[1.
c:s b:3.507295487 m:_0_ g:tcp h:1040 q:[0 i:0 o:0 j:0]k:------- l:[0:0 y:1:0 n:32 p :0] r:[2.
c:r b:3.507295487 m:_0_ g:tcp h:1040 q:[0 i:0 o:0 j:0]k:------- l:[0:0 y:1:0 n:32 p :0] r:[2.
c:s b:3.507295487 m:_0_ g:tcp h:1060 q:[0 i:0 o:0 j:0]k:------- l:[0:0 y:1:0 n:32 p :1] r:[2.
c:r b:3.507584536 m:_1_ g:ACK h:38 q:[0 i:1 o:0 j:0]k:------- l:[0:0 y:1:0 n:32 p :1] r:[2.
c:s b:3.507674487 m:_0_ g:RTS h:44 q:[256e i:1 o:0 j:0]k:------- l:[0:0 y:1:0 n:32 p :1] r:[2.
c:s b:3.775336507 m:_0_ g:RTS h:44 q:[256e i:1 o:0 j:0]k:------- l:[0:0 y:1:0 n:32 p :1] r:[2.
c:s b:6.033650740 m:_0_ g:tcp h:1118 q:[13a i:1 o:0 j:800]k:------- l:[0:0 y:1:0 n:32 p :1] r:[176.
The problem lies in the lines that stops at j (2,10,11,12), it starts to duplicate the rest of the above line which results in miscounting
for example
if i tried to count the l variable
if(l =="[0:0")
zero++;
o/p
zero = 10.
The result should be 7 when counted manually .. any ideas how can i make it at lines(2,10,11,12) stop at j and count the rest normally
thanx in advance
Topic archived. No new replies allowed.