Problem using strcmp and strcopy, strlen

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




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cstring>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>

			using namespace std;
int main() {
    
        char type;
        string line;
        int numlines = 0;
        double time; 
        char sequenceno1[5], sequenceno2[5],T[5];
        string s1,s2,s3,s4,s5,s6,s7,s8;
        int result;

ifstream myfile ("TTEST.tr");
ofstream MYFILE8("node7-8.tr");
ofstream MYFILE9("node3-4.tr");


 if(myfile.is_open()) {

		while (getline(myfile,line)) {
	     			numlines++;
				istringstream input(line);
    
    input >> type >> time;

switch (type)
{
	
case 's':
        input >> s1 >> s2 >> s3 >> sequenceno1 >> s4 >> s5 >> s6 >> s7 >> s8;
//cout << "s1 =" << s1 << "s2 = " << s2  << "s3 = " << s3 << "sequenceno1 =" << sequenceno1 << " s4 = " << s4  << " s5=  "<< s5 << "s6 =" << s6 << "s7 = " << s7 << "s8 = " << s8 <<".\n";
  		strcpy(T,sequenceno1);
			cout << T << ".\n"; // correct value of T 
			break;
			
case 'r': 
      input >> s1 >> s2 >> s3 >> sequenceno2 >> s4 >> s5 >> s6 >> s7 >> s8;
//cout << "s1 =" << s1 << "s2 = " << s2  << "s3 = " << s3 << "sequenceno2 =" << sequenceno2 << " s4 = " << s4  << " s5=  "<< s5 << "s6 =" << s6 << "s7 = " << s7 << "s8 = " << s8 <<".\n";
      
	result = int (strcmp (T,sequenceno2) == 0);
cout << "sequenceno1 = " << T << "     " << "sequenceno2 = " << sequenceno2 << "      " <<"result = " << result << ".\n"; 

		if (result == 0) {// if both strings are equal execute the following code //
					if(type == 's') {
						if ((s1== "_7_")&&(s7== "8"))
						if ((s2 == "MAC")&&(s4== "tcp"))
								MYFILE8 << line << ".\n";
										}
					if (type == 'r') {
 						if ((s1 == "_8_")&&(s8 == "7"))
						if ((s2 == "MAC")&&(s4 == "tcp"))
								MYFILE8 << line << ".\n";
								}
								
					if(type == 's') {
						if ((s1 == "_3_")&&(s7 == "4"))
						if ((s2 == "MAC")&&(s4 == "tcp"))
								MYFILE9 << line << ".\n";
										}
					if (type == 'r') {
 						if ((s1 == "_4_")&&(s8 == "3"))
						if ((s2 == "MAC")&&(s4 == "tcp"))
								MYFILE9 << line << ".\n";
								}
								
							break;
						}	
		

}


}
			


myfile.close();

}				


else cout << "Unable to open file";


return 0;


}



here's the output of the program

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
Last edited on
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.
Last edited on
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
Last edited on
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.
ok i am sry .. is it better to use
int 
or just use
 strcmp (T,sequenceno2) == 0
I did try removing the int it did give me the same result.
I am sry one more thing
when I changed my condition to
 
if (result != 0)

The s lines were not copied to another file onlyon the other hand, the r lines were copied.... any ideas why

This is my output file

r 167.209410087 _8_ MAC  --- 29867 tcp 572 [13a 8 7 800] ------- [7:2 8:1 32 8] [330 0] 1 1...
r 167.217714923 _8_ MAC  --- 29868 tcp 572 [13a 8 7 800] ------- [7:2 8:1 32 8] [331 0] 1 1...
r 170.003942963 _8_ MAC  --- 30344 tcp 572 [13a 8 7 800] ------- [7:3 9:0 32 8] [147 0] 1 1...
r 170.168029172 _8_ MAC  --- 30466 tcp 572 [13a 8 7 800] ------- [7:2 8:1 32 8] [334 0] 1 1...
r 170.186522463 _8_ MAC  --- 30467 tcp 572 [13a 8 7 800] ------- [7:2 8:1 32 8] [335 0] 1 1...

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?
ok here's another attempt but still not getting the s lines only the r lines as I don't know how to
 Save last line into string (or stringstream) and output it if it is needed 
can u clarify it more

here's the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <cstring>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>

			using namespace std;
int main() {
    
        char type;
        string line;
        int numlines = 0;
        double time; 
        //char sequenceno1[6], sequenceno2[6],T[6];
        string s1,s2,s3,sequenceno1,sequenceno2,s4,s5,s6,s7,s8,T;
        int result;

ifstream myfile ("TTEST.tr");
ofstream MYFILE8("node7-8.tr");
ofstream MYFILE9("node3-4.tr");


 if(myfile.is_open()) {

		while (getline(myfile,line)) {
	     			numlines++;
				istringstream input(line);
    
    input >> type >> time;

switch (type)
{
	
case 's':
        input >> s1 >> s2 >> s3 >> sequenceno1 >> s4 >> s5 >> s6 >> s7 >> s8;
        T = sequenceno1; // I tried saving it this way 
			break;
			
case 'r': 
      input >> s1 >> s2 >> s3 >> sequenceno2 >> s4 >> s5 >> s6 >> s7 >> s8;
      cout << "sequenceno1= " << sequenceno1 << "          " << "sequenceno2 =" << sequenceno2 << ".\n";
	
		if (T == sequenceno2) {// if both strings are equal execute the following code //
					if(type == 's') {
						if ((s1 == "_7_")&&(s7== "8"))
						if ((s2 == "MAC")&&(s4== "tcp"))
								MYFILE8 << line << ".\n";
										}
					if (type == 'r') {
 						if ((s1 == "_8_")&&(s8 == "7"))
						if ((s2 == "MAC")&&(s4 == "tcp"))
								MYFILE8 << line << ".\n";
								}
								
					if(type == 's') {
						if ((s1 == "_3_")&&(s7 == "4"))
						if ((s2 == "MAC")&&(s4 == "tcp"))
								MYFILE9 << line << ".\n";
										}
					if (type == 'r') {
 						if ((s1 == "_4_")&&(s8 == "3"))
						if ((s2 == "MAC")&&(s4 == "tcp"))
								MYFILE9 << line << ".\n";
								}
								
							break;
						}	
		

}


}
			


myfile.close();

}				


else cout << "Unable to open file";


return 0;


}
can u plz check if it is better than the previous code 
thank u 
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.
@MiiNiPaa thank u 4 your response, well I some how seem to be stuck in this part ..
You should create another variable and prepare your s-line 
do u mean I need to create another variable other than
 type
to store my s
I am sry I am bit confused ...can u plz provide me with a simple code example on this part or any link that helps
thank u

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//BEGORE
case 's':
    input >> s1 >> s2 >> type >> s3 >> s4;
    break;
case 'r'
    input >> s1 >> s2 >> type >> s3 >> s4;
    if (type == 's')
        out << "_xxx_" << line;
    if (type == 'r')
        out << " ! ! " << line;

//AFTER
std::string oldline;
//...
case 's':
    input >> s1 >> s2 >> type >> s3 >> s4;
    oldline = "_xxx_" + line; //saving line for future use
    break;
case 'r'
    input >> s1 >> s2 >> type >> s3 >> s4;
    out << oldline; //output s-line first
    out << " ! ! " << line; //then r-line 
Thank u 4 your quick response ... well I did give it a try ... its printing the correct s lines at line 15 ... but at line 18
if(type == 's') {
... the s lines are not printed out or written to my file ... plz help me out... don't know what i am doing wrong :'(
well here's my attempt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
case 's':
        input >> s1 >> s2 >> s3 >> sequenceno1 >> s4 >> s5 >> s6 >> s7 >> s8;
        oldline = line; 
        //cout << oldline << ".\n";
			break;

case 'r': 
//cout << oldline << ".\n";
      input >> s1 >> s2 >> s3 >> sequenceno2 >> s4 >> s5 >> s6 >> s7 >> s8;
     // cout << "sequenceno1= " << sequenceno1 << "          " << "sequenceno2 =" << sequenceno2 << ".\n";
     //cout << oldline << ".\n";
      
		if (sequenceno1 == sequenceno2) {// if both strings are equal execute the following code //
		//cout << "sequenceno1= " << sequenceno1 << "          " << "sequenceno2 =" << sequenceno2 << ".\n";
		cout << oldline << ".\n"; // printing out the correct s lines 
	
					if(type == 's') {
					cout << "oldline" << ".\n"; // Not printing any s lines 
						if ((s1 == "_7_")&&(s7== "8"))
						if ((s2 == "MAC")&&(s4== "tcp"))
							MYFILE8 << oldline << ".\n";
										}
					if (type == 'r') {
 						if ((s1 == "_8_")&&(s8 == "7"))
						if ((s2 == "MAC")&&(s4 == "tcp"))
								MYFILE8 << line << ".\n";
								}
								

thank u
1
2
3
case 'r': 
//...
if(type == 's') {
Type cannot be s at this point!. It is equivalent to
1
2
if(type == 's')
    if (type == 'r')
You need all code related to s line forming to your s case. And output s line just before r one.
ok what should i do it has to be the s character that determines my line because there are different characters in my trace file :(
well I did give it a try as u mentioned ... here 's what I have come up with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
case 's':
        input >> s1 >> s2 >> s3 >> sequenceno1 >> s4 >> s5 >> s6 >> s7 >> s8;
        oldline = line;
        stype = type; 
			break;
			
case 'r':
      input >> s1 >> s2 >> s3 >> sequenceno2 >> s4 >> s5 >> s6 >> s7 >> s8;
      Oldline =line; 
 //cout << "sequenceno1= " << sequenceno1 << "          " << "sequenceno2 =" << sequenceno2 << ".\n";
   // cout << oldline << ".\n";
      
if (sequenceno1 == sequenceno2) {// if both strings are equal execute the following code //
	cout << "sequenceno1= " << sequenceno1 << "          " << "sequenceno2 =" << sequenceno2 << ".\n";
	
	if (stype == 's') {
	 MYFILE8 << oldline << ".\n";// printing out the s lines
									}
	if (type == 'r') {
		MYFILE8 << line << ".\n";
								}

unfortunately not printing it out after

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



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.


I just need to know how can I check if the s line is followed by r line ...
thank u
Last edited on
Topic archived. No new replies allowed.