In: Computer Science
Hi!
I am trying to compare 2 files, and figure out which lines from infile2 are also in infile1, and output the line index, from infile2, of common lines. I would also want to see which lines (actual string line) are common.
Below is my c++ program, and I am not sure what I am doing wrong. Can someone tell me what I am doing wrong and how it can be fixed?
_________________________________________________________main.cpp
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
int main(int argc, char *argv[]){
string myString1, myString2;
vector<string> string1, string2;
int i = 0;
int y = 0;
ifstream infile1("LeftShift2.txt");
ifstream infile2("LeftShift3.txt");
if(infile1.is_open() && infile2.is_open())
{
while(!infile1.eof() && !infile2.eof())
{
getline(infile1, myString1);
string1.push_back(myString1);
getline(infile2, myString2);
string2.push_back(myString2);
if(string1.at(i) != string2.at(y))
{
y++;
}
else if(string1.at(i) == string2.at(y))
{
i++;
}
}
cout<<"common line is at: " << y << endl;
cout<<"The common lines are: "<< string2.at(y) << endl;
}
infile1.close();
infile2.close();
return 0;
}
__________________________________________LeftShift2.txt
Adios amiga mia
Bella ciao
Si le monde etait aussi petit
On ne serait pas aussi grandiose
Pays du lait et du miel
Alors, on danse?!
-------------------------------------------------------------LeftShift3.txt
Pays du froid et des montagnes
Burundi
Pays du lait et du miel
Adios amiga mia
-------------------------------------------------------------Output (assume count line start at 0)
common line is at: 2
The common lines are: Pays du lait et du miel
common line is at: 3
The common lines are: Adios amiga mia
If you have any doubts, please give me comment....
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string myString1, myString2;
vector<string> string1;
int i = 0;
int y = 0;
ifstream infile1("LeftShift2.txt");
ifstream infile2("LeftShift3.txt");
if (infile1.is_open() && infile2.is_open())
{
while (!infile1.eof())
{
getline(infile1, myString1);
string1.push_back(myString1);
}
int i=0;
while(!infile2.eof()){
getline(infile2, myString2);
for(int j=0; j<string1.size(); j++){
if(myString2.compare(string1.at(j))==0){
cout << "common line is at: " << i << endl;
cout << "The common lines are: " << myString2 << endl;
}
}
i++;
}
}
infile1.close();
infile2.close();
return 0;
}