Compare and combine two different text files

hai guys...

i'm facing a problem on how to compare and then combine back the two different files (text files)

i have 2 text files which i stored them in 2D array

this is my first text files. I named this file as text 1
0 1 2
1 2 3
3 2 1

3 1 1 1
3 1 1 1
3 1 1 1

and here is my text files number 2

1 2 3
3 2 1

what I plan to do is to compare this 2 files .
If the numbers in text 1 exist in text 2, then the number below in text 1 will be change to 4

for example :
In text 1, there is 0 1 2 , but in text 2, 0 1 2 doesn't exist , so 3 is remained and display

next, in text 1, there is 1 2 3,and in text 2, 1 2 3, exist, therefore 3 in bold will be turned to 4 and display

The output should be like this :

0 1 2
1 2 3
3 2 1

3 1 1 1
4 1 1 1
4 1 1 1


And same to others

I have tried many times to code this. But I cannot got as I want.
I hope anyone here can help me.

tqvm..


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

#include<iostream>
#include<fstream>

using namespace std;

void main()
{
int no1,no3,**m,**n,**t,dummy;
ifstream infile1;
infile1.open(text1.txt);

ifstream infile2;
infile2.open("text2.txt");

infile1 >> no1;
infile1 >> no1 ;

infile2 >> no3;

m = new int *[no1];
for(int i=0;i<no1;i++)
m[i] = new int[3];

n = new int *[no1];
for(int i=0;i<no1;i++)
n[i] = new int[3];

t = new int *[no3];
for(int i=0;i<no3;i++)
t[i] = new int[3];

for(int i=0;i<no1;i++)
{
infile1 >> m[i][0] >> m[i][1] >> m[i][2]
infile1 >> dummy >> n[i][0] >> n[i][1] >> n[i][2];
}

for(int j=0;j<no3;j++)
infile2 >> t[i][0] >> t[i][1] >> t[i][2];

for(int i=0;i<no1;i++)
{
for(int j=0;j<no3;j++)
{
if(m[i][0] == t[j][0] && m[i][1] == t[j][1] && m[i][2] == t[j][2])
{
dummy = 4;
cout << dummy << endl;
}
else
{
dummy =3;
cout << dummy << endl;
}
}
}

}


This is my code, and I hope anyone here can help me by let me know, what is wrong with my code...

tqvm again ... pliss~~
Last edited on
I don't quite understand why the output needs to be as it is, but to do this you need to compare yes?
So you need to work out a way to do comparison, the rest is trivial.

The below kinda confuses me, as shit just isn't well named.
I'd be using getline() to grab stuff out of the file, makes life easy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for(int i=0;i<no1;i++)
{
   for(int j=0;j<1;j++)
   {
      if((m[i][j] == t[i][j]) && (m[i][j+1] == t[i][j+1]) && (m[i][j+2] == t[i][j+2]))
      {
         dummy = 4;
         cout << dummy << endl;
      }
      else
      {
         dummy =3;
         cout << dummy << endl;
      }
   }
}


Might be a bit better (or may not be anything like what your trying to do).
Perhaps you may want to take a look at this:
http://cplusplus.com/forum/articles/17108/
thank you for response ...

actually there's some changes in the output.
i'm not familiar with getline, that's why i used 2D array to grab stuff out of the file ...

what i mean in that code is ..

first loop is for text 1,
second loop is for text 2,
then, the comparison takes place
if the stuff in text 1 is equal as in text 2,
then dummy will change to 4 and display
else
dummy will remain 3 and display
end loop second
end loop first



but at 'else' there is problem there... but don't have any idea, on how to deal with this...

hope u can understand my explanation and can give any idea..

tq vm...
Well then I think the code snipit I posted should work.
Although the second loop is not necessary, you will notice the second argument i changed to: j<1
Last edited on
Topic archived. No new replies allowed.