I am trying to implement something that resembles a "brute string search algorithm", only that I am matching strings instead of characters. I've got two files (i.e. file A and file B), the intention is to read the first line from file A and then parse the entire file B to see if that line is contained in file B. If the line is not contained in file B, the program must move on to line number 2 in file A and the process is repeated until the end of file A.
{
QFile outputFile
(output_file
);
{
qDebug() << "file1 not opened";
}
{
qDebug() << "file2 not opened";
}
if(!outputFile.
open(QFile::WriteOnly |
QFile::Text)) {
qDebug() << "output file not opened";
}
while(!in_file1.atEnd())
{
qDebug() << "Looping" <<endl;
QString file1_str
= in_file1.
readLine(0);
QString temp_file1_str
= file1_str.
mid(0, file1_str.
indexOf('.'));
while(!in_file2.atEnd())
{
qDebug() << "**Inner Loop**" <<endl;
QString file2_str
= in_file2.
readLine(0);
QString temp_file2_str
= file2_str.
mid(0, file2_str.
indexOf('.'));
if(temp_file2_str == temp_file1_str)
{
qDebug() << "found" <<endl;
out << file2_str <<endl;
qDebug() << temp_file2_str <<endl;
file2_str.clear();
break;
}
file2_str.clear();
}
file1_str.clear();
}
file1.close();
file2.close();
outputFile.close();
}
void crossRefernce(QString input1, QString input2, QString output_file)
{
QFile file1(input1);
QFile file2(input2);
QFile outputFile(output_file);
if(!file1.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "file1 not opened";
}
if(!file2.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "file2 not opened";
}
if(!outputFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "output file not opened";
}
QTextStream in_file1(&file1);
QTextStream in_file2(&file2);
QTextStream out(&outputFile);
while(!in_file1.atEnd())
{
qDebug() << "Looping" <<endl;
QString file1_str = in_file1.readLine(0);
QString temp_file1_str = file1_str.mid(0, file1_str.indexOf('.'));
while(!in_file2.atEnd())
{
qDebug() << "**Inner Loop**" <<endl;
QString file2_str = in_file2.readLine(0);
QString temp_file2_str = file2_str.mid(0, file2_str.indexOf('.'));
if(temp_file2_str == temp_file1_str)
{
qDebug() << "found" <<endl;
out << file2_str <<endl;
qDebug() << temp_file2_str <<endl;
file2_str.clear();
break;
}
file2_str.clear();
}
file1_str.clear();
}
file1.close();
file2.close();
outputFile.close();
}
To copy to clipboard, switch view to plain text mode
My problem is that, the output of the above code indicates that the inner while loop iterates only 3 times and never executes again. The ultimate goal is to delete similar entries in file A and B and leave unique values. Your help will be greatly appreciated.
Bookmarks