0

I am given a column of strings in a text file and I have to compare them with each other - I want to compare the first string with all below it, then go back to the second one and compare it with all below it and so on. The problem is I have no idea how to write the code for it

Bhoke
  • 468
  • 6
  • 22
unfi
  • 3
  • 4
  • You should do some research before posting question here. There are many tutorials on the web, you should just search for `C++ read text file` :http://www.cplusplus.com/doc/tutorial/files/ and you should compare the string with `strcmp` in a loop – Bhoke Nov 05 '16 at 11:23
  • I have read these, the thing is - once I have compared the first string to all others, how do I get back to the second one, how do i get the pointer there, I should use seekp() or seekg() but this is pretty much where i get stuck – unfi Nov 05 '16 at 11:34
  • You must start with something first. **Then** come here. – Christian Hackl Nov 05 '16 at 14:27

2 Answers2

1

Using a nested loop does what you expect;

#include <iostream>
#include <fstream>
#include <vector> //include this to use vector

using namespace std;

int main() {

    //to take input from the file
    ifstream fin;

    //to read the same strings into 2 arrays so we can loop it appropriately
    //by taking one string and comparing it to all below it.
    vector <string> line1;
    vector <string> line2;

    //to hold a line of string
    string temp;

    //replace this with with your file 
    fin.open("hello.txt");

    //to check if file cannot be opened or does not exist
    if(!fin.is_open()) {
        cout << "file could not be opened";
    }

    //strings are inserted into element of these 2 vectors 
    //(Internally, vectors use a dynamically allocated array to store their elements in adjacent memory locations)
    //that is why i decided to use vectors. Also, using the push_back method
    //to insert the strings into both arrays means we don't have to specify the size of the array
    while (getline(fin, temp)) {
        line1.push_back(temp);
        line2.push_back(temp);
    }


    //nested loop is used to make sure one string is used to operate 
    //on all the strings in the file and move to the next to do same
    //and so on...
    for (unsigned int i = 0; i < line1.size(); i++) {
        for (unsigned int j = 0; j < line2.size(); j++) {
            //you can compare first string with all below here however you want to do it
            //I just did this so you see how it behaves
            cout << line1[i] << " = " << line2[j] << endl;
        }
    }

    return 0;
}
0

THE MOST EASY WAY TO DO it is using cmd linux like a grep:

// 1 way

 grep -w -v -f file1.log file.2 > mach.log

// 2 way

grep -w -f file1.log file.2 > mach.log

You mustn't forget the flag mean:

-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

-v, --invert-match Invert the sense of matching, to select non-matching lines.

-f FILE, --file=FILE Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e (--regexp) option, search for all patterns given. The empty file contains zero patterns, and therefore matches nothing.