Jan 7, 2019 at 9:08pm UTC
hello guys,
I just want to compare two files. Then i want to display the same strings on the screen. I have strings in these files. Each strings are on the different lines. I wrote that code but it didnt work true. Where is my wrong can you help me?
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
int main()
{
string myArray1[1000];
int loop1 = 0;
string line1;
ifstream myfile1("davetlilistesi.txt" );
if (myfile1.is_open())
{
while (!myfile1.eof()) {
getline(myfile1, line1);
myArray1[loop1] = line1;
loop1++;
}
myfile1.close();
}
else
cout << "Unable to open file" <<endl;
string myArray2[1000];
int loop2 = 0;
string line2;
ifstream myfile2("davetlierkek.txt" );
if (myfile2.is_open())
{
while (!myfile2.eof()) {
getline(myfile2, line2);
myArray2[loop2] = line2;
loop2++;
}
myfile2.close();
}
else
cout << "Unable to open file" << endl;
string myArray3[1000];
int matches = 0;
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if (myArray1[i] == myArray2[j]) {
cout << "Match " << ++matches << ": " << myArray1[i] << endl;
myArray3[i] = myArray1[i];
}
}
}
string line3;
ofstream myfile3("ortaklar.txt" );
if (myfile3.is_open())
{
for (int count = 0; count < 1000; count++) {
myfile3 << myArray3[count] << " " ;
}
myfile3.close();
}
else
cout << "file is not open" << endl;
cin.get();
return 0;
}
Last edited on Jan 8, 2019 at 11:49am UTC
Jan 7, 2019 at 9:29pm UTC
what did not work?
you did not do anything about case, is the case in both files identical for all words? Remember cat and Cat won't match...!
why not use vectors of strings instead of fixed sized array?
there are far better ways to do this than brute force, but we can skip that for now.
Jan 8, 2019 at 5:03am UTC
Please edit your post to add [cod e][/code] tags (the <> format on the right) around your code.
Jan 8, 2019 at 1:59pm UTC
At line 43 you're using myArray1's index to insert into myArray3. So if lines 1,4 and 544 of the first file match, then you'll put the match strings in positions 1, 4, and 544 of myArray3. I suspect that isn't what you want. To fix this, use a separate variable for the index into myArray3.