How to compare files from a columns text file?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 562 Views - Last Post: 01 March 2018 - 02:10 AM Rate Topic: -----

#1 abas2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 18

How to compare files from a columns text file?

Posted 22 February 2018 - 07:43 PM

I have a question on how to compare a 2 file from a columns text file. so far i only manage to create a program to compare lines but not in columns. I have no idea how to create the program where for example the user wants to compare second columns on the first file and the third columns on the second file.
Here the text file:
First file:
1| 1111| Ali
2| 2222| Abu
3| 3333| Ahmad
4| 6666| June

second file:
1| 11| 1111| Ali
2| 22| 3333| Ahmad
3| 33| 8888| Judy
4| 44| 7777| John
5| 55| 6666| June
6| 66| 9999| Fatimah

Is This A Good Question/Topic? 0
  • +

Replies To: How to compare files from a columns text file?

#2 abas2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 18

Re: How to compare files from a columns text file?

Posted 22 February 2018 - 07:58 PM

sorry the text files are not separated by space. it should be like this
first text file:
1|1111|Ali
2|2222|Abu
3|3333|Ahmad
4|6666|June

second text file:
1|11|1111|Ali
2|22|3333|Ahmad
3|33|8888|Judy
4|44|7777|John
5|55|6666|June
6|66|9999|Fatimah

thats why im having trouble. i dont think i can use array since its not seperated by space.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: How to compare files from a columns text file?

Posted 22 February 2018 - 08:19 PM

You can read each file 1 line at a time and split ("tokenize") each line into a separate string representing each column.

You didn't mention what language you're writing. C? C++?
Was This Post Helpful? 0
  • +
  • -

#4 abas2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 18

Re: How to compare files from a columns text file?

Posted 22 February 2018 - 08:20 PM

View Postr.stiltskin, on 22 February 2018 - 08:19 PM, said:

You can read each file 1 line at a time and split ("tokenize") each line into a separate string representing each column.

You didn't mention what language you're writing. C? C++?

sorry, im writing a C++.
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: How to compare files from a columns text file?

Posted 22 February 2018 - 09:05 PM

So you can use getline (see constructor version #2 on that page) to read one line of file input to a std::string.

Then you can take that string and put it into an istringstream using the istringstream::str function.

Then you can use getline again (see constructor #1 using with your '|' delimiter) to parse the line from the istringstream into separate columns.

And so on ...
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,064
  • Joined: 25-December 09

Re: How to compare files from a columns text file?

Posted 22 February 2018 - 09:08 PM

Quote

i dont think i can use array since its not seperated by space.

There is no reason you can't use arrays (or preferably a vector) to hold the information. I suggest using a structure to hold the information from each file then a vector/array of those structures.

By the way you need to show how you're attempting to read the file before I can give you specific examples.


Jim
Was This Post Helpful? 0
  • +
  • -

#7 abas2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 18

Re: How to compare files from a columns text file?

Posted 22 February 2018 - 11:28 PM

View Postjimblumberg, on 22 February 2018 - 09:08 PM, said:

Quote

i dont think i can use array since its not seperated by space.

There is no reason you can't use arrays (or preferably a vector) to hold the information. I suggest using a structure to hold the information from each file then a vector/array of those structures.

By the way you need to show how you're attempting to read the file before I can give you specific examples.


Jim

the program is from my previous works. My lecturer told me to modify it so it can compare the files that have a column.
This is my previous works

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){
	ifstream File1;
	ifstream File2;
	ofstream File3;
	string line1,line2;
	
	File1.open("A.txt");
	File2.open("B.txt");
	File3.open("OutputA.txt");
	if(File1.fail()){ cerr<<"Error opening file !!"<<endl;exit(1);}
	if(File2.fail()){ cerr<<"Error opening file !!"<<endl;exit(1);}
	
	while(getline(File1,line1)){
		bool found(false);
		while(getline(File2,line2)){
			if(line1==line2){
				found = true;
				File3<<line1<<"	Found"<<endl;
			}
		}
		if(!found){
			File3<<line1<<"	Not Found"<<endl;
		}
		File2.clear();
		File2.seekg(0,ios::beg);
	}
	File1.close();
	File2.close();
	File3.close();
	
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#8 Thomas1965   User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 366
  • Joined: 09-September 16

Re: How to compare files from a columns text file?

Posted 23 February 2018 - 01:20 AM

You can read the second column in file1 like this:
  int num;
  char sep;
  string name;
  while (src >> num // reads the first number until the first |
    >> sep  // reads the |
    >> num) // reads the second number until the next |
            // add num to vector
    getline(src, name); // read the rest of the line
    cout << num << "\n";


Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,465
  • Joined: 05-May 12

Re: How to compare files from a columns text file?

Posted 23 February 2018 - 01:27 AM

abas2: There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

#10 abas2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 18

Re: How to compare files from a columns text file?

Posted 26 February 2018 - 01:25 AM

i just finished my coding but it seems theres a problem with my first Array. It doesn't loop.

#include<iostream>
#include<fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	ifstream File1;
	ifstream File2;
	ofstream File3;
	File1.open("A.txt");
	File2.open("B.txt");
	File3.open("Output.txt");
	string line1,line2;
	if(File1.fail()){
		cerr<<"Error opening File!!"<<endl;
		exit(1);
	}
	if(File2.fail()){
		cerr<<"Error opening File!!"<<endl;
		exit(1);
	}
	while(getline(File1,line1)){
		replace(line1.begin(),line1.end(),'|',' ');
		vector<string>array1;
		stringstream ss(line1);
		string temp;
		while(ss>>temp){
			array1.push_back(temp);
		}
		while(getline(File2,line2)){
			replace(line2.begin(),line2.end(),'|',' ');
			vector<string>array2;
			stringstream ss2(line2);
			string temp2;
			while(ss2>>temp2){
				array2.push_back(temp2);
			}
			cout<<"A "<<array1.at(1)<<" B "<<array2.at(2)<<endl;
		}
		
	}
	File1.close();
	File2.close();
	File3.close();
	
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#11 Thomas1965   User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 366
  • Joined: 09-September 16

Re: How to compare files from a columns text file?

Posted 26 February 2018 - 01:37 AM

Why so complicated?
Create a function
vector<string> read_file(const string& filename)
and call it for both input files.
This will give you 2 vectors which makes it easy to compare and find.
Was This Post Helpful? 1
  • +
  • -

#12 abas2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 18

Re: How to compare files from a columns text file?

Posted 26 February 2018 - 09:03 PM

when using the vector, how do i specify specific column so i can compare between them? for example i want to compare column 2 in the first file and column 3 in the second file.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,064
  • Joined: 25-December 09

Re: How to compare files from a columns text file?

Posted 26 February 2018 - 09:26 PM

Have you considered using a struct/class to hold the data from the file? Then you can refer by "name" to the desired field of each file.

Perhaps something like:

struct Data_1
{
    int line_number;
    int id;
    std::string name;
};

struct Data_2 : public Data_1
{
    int some_data;
};





Jim
Was This Post Helpful? 0
  • +
  • -

#14 abas2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 18

Re: How to compare files from a columns text file?

Posted 26 February 2018 - 11:51 PM

i just finished my coding but im not sure if this is the best way to do it. i hope anyone can help me fix my code.

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

vector<string> open_file(const char* file_name)
{
    vector<string> collection;
    ifstream file(file_name);
    string line;
	
    if(file.is_open()) {

		while(getline(file,line)){
			istringstream ss(line);
			while(getline(ss,line,'|')){
			collection.push_back(line);
			
			}
		}
        file.close();
    }
    return collection;
}  

int main()
{
    vector<string> collection1 = open_file("file1.txt");
    vector<string> collection2 = open_file("file2.txt");
	ofstream File3;
	File3.open("file3.txt");
    vector<string> results;
	int i=0;
	cout<<"What column number :";
		cin>>i;
		i=i-1;

   while( i < collection1.size()) {
		bool is_found(false);

        for(size_t j = 0; j < collection2.size(); j++) {

            if(collection1.at(i)==collection2.at(j)) {
				is_found = true;
                results.push_back(collection1.at(i) + " FOUND!");
                break;
            }
		}
		if(!is_found){
		results.push_back(collection1.at(i)+" NOT FOUND!");
		}
		i = i+2;
		i++;
	}
    for(size_t i = 0; i < results.size(); i++) {
        cout << results.at(i) << '\n';
		File3<<results.at(i)<<endl;
    }

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#15 Thomas1965   User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 366
  • Joined: 09-September 16

Re: How to compare files from a columns text file?

Posted 27 February 2018 - 01:23 AM

View Postabas2, on 27 February 2018 - 04:03 AM, said:

when using the vector, how do i specify specific column so i can compare between them? for example i want to compare column 2 in the first file and column 3 in the second file.

From the first file you read only the second column and store it in the vector.
From the second file you read only the third column and store it in the vector.
This gives you two vectors of ints.
Also there is std::find in the STL, no need to write your own search function.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2