hey all
I am facing a problem when I tried using strcmp, my program is suppose to compare two strings and check if they are equal based on this equality, several commands should be executed when this condition is true, well when googling strcmp I found out if the result = 0 then both strings are equal
here's a snap shot of the trace file I am using
s 167.204369958 _7_ MAC --- 29867 tcp 630 [13a 8 7 800] ------- [7:2 8:1 32 8] [330 0] 0 1..
r 167.209410087 _8_ MAC --- 29867 tcp 572 [13a 8 7 800] ------- [7:2 8:1 32 8] [330 0] 1 1..
s 167.212674795 _7_ MAC --- 29868 tcp 630 [13a 8 7 800] ------- [7:2 8:1 32 8] [331 0] 0 1..
r 167.217714923 _8_ MAC --- 29868 tcp 572 [13a 8 7 800] ------- [7:2 8:1 32 8] [331 0] 1 1..
D 167.240723594 _8_ MAC BSY 0 RTS 44 [162e 8 7 0] ..
D 167.243783594 _8_ MAC BSY 0 RTS 44 [162e 8 7 0] ..
s 169.998902881 _7_ MAC --- 30344 tcp 630 [13a 8 7 800] ------- [7:3 9:0 32 8] [147 0] 0 1..
r 170.003942963 _8_ MAC --- 30344 tcp 572 [13a 8 7 800] ------- [7:3 9:0 32 8] [147 0] 1 1..
s 170.162989092 _7_ MAC --- 30466 tcp 630 [13a 8 7 800] ------- [7:2 8:1 32 8] [334 0] 0 1..
r 170.168029172 _8_ MAC --- 30466 tcp 572 [13a 8 7 800] ------- [7:2 8:1 32 8] [334 0] 1 1..
s 170.181482383 _7_ MAC --- 30467 tcp 630 [13a 8 7 800] ------- [7:2 8:1 32 8] [335 0] 0 1..
r 170.186522463 _8_ MAC --- 30467 tcp 572 [13a 8 7 800] ------- [7:2 8:1 32 8] [335 0] 1 1..
s 178.013133357 _7_ MAC --- 32131 tcp 630 [13a 8 7 800] ------- [7:2 8:1 32 8] [340 0] 0 1..
r 180.725285180 _8_ MAC --- 32716 tcp 572 [13a 8 7 800] ------- [7:2 8:1 32 8] [341 0] 1 1..
s 18.418916379 _6_ MAC --- 47 tcp 630 [13a 8 6 800] ------- [6:3 8:0 32 8] [1 0] 0 1
r 18.423956570 _8_ MAC --- 47 tcp 572 [13a 8 6 800] ------- [6:3 8:0 32 8] [1 0] 1 1
The program should compare sequenceno1 and sequenceno2 for example
sequenceno1 = 30467 sequenceno2 = 30467 // condition is true copy the line in another file
sequenceno1 = 32131 sequenceno2 = 32716 // condition is false shouldnot copy the line
sequenceno1 = 2986729867 sequenceno2 = 29867 result = 0.
sequenceno1 = 2986829868 sequenceno2 = 29868 result = 0.
sequenceno1 = 3034430344 sequenceno2 = 30344 result = 0.
sequenceno1 = 3046630466 sequenceno2 = 30466 result = 0.
sequenceno1 = 3046730467 sequenceno2 = 30467 result = 0.
sequenceno1 = 3213132716 sequenceno2 = 32716 result = 0.
This should be the correct output
sequenceno1 = 29867 sequenceno2 = 29867 result = 0.
sequenceno1 = 29868 sequenceno2 = 29868 result = 0.
sequenceno1 = 30344 sequenceno2 = 30344 result = 0.
sequenceno1 = 30466 sequenceno2 = 30466 result = 0.
sequenceno1 = 30467 sequenceno2 = 30467 result = 0.
sequenceno1 = 32131 sequenceno2 = 32716 result = 1.// condition is false
why is the result equal to zero though the two numbers are not equal and y is the values of sequenceno1 duplicated with sequenceno2 ?
one more thing the size of the above number varies I know it has something to do with strlen to make it general but don't know how to use in the program plz help me out its urgent
thanx in advance
char sequenceno1[5]
This variabe can hold only 4 characters + trailing 0. You are trying to store 5 characters which leads to buffer overflow and other sort of nasty behavior. Remember: always leave place for trailing 0 when using c-strings.
Edit: same with other variables.
Thank u 4 ur reply its working but I am getting the result equals to 1 if they are equal and 0 if not equal shouldn't it be reversed 0 if equal and 1 if not
plz help
Line 46: strcmp (T,sequenceno2) == 0 will return boolean true if strcmp returns 0 (when they are equal) and false otherwise. Then you cast it into int where true maps to 1 and false to 0.
It is because of your program flow: when you found out that two sequenceno variables are equal, you have already overwrote data you got from 's' line. Save last line into string (or stringstream) and output it if it is needed.
Actually I do not see, why you took all this troubles with strcmp, seeing as you already used std::string in your program. Why not change sequenceno variables to string and just use normal assigment and normal comparsion?
TO elaborate: code where you check for type is inside switch(type) case'r':In other words, it cannot been anything but r You should create another variable and prepare your s-line (all you did in if(type = 's')) in case 's'. Then in your case 'r' you just print that variable without need to access previous variables.
Advice: if you have 8 level of nesting, it is a good sign that you should move part of your code into function.
if ((m== "_7_")&&(i== "8"))
if ((d == "MAC")&&(g== "tcp"))
well I am facing another problem I did check for the sequence no but the program is suppose to read a specific time intervals for ex from 160-170 but due to the 9 number precision in the file I am reading the 170.003942963 is considered in the second interval (170-180) ... therefore the following two lines should be removed as the s line is not followed by r line in this interval the same is applied to r line as it doesnot have s line above it ... These two lines are causing incorrect calculations ... plz help