How to compare files from a columns text file?

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 27 February 2018 - 06:08 PM

View PostThomas1965, on 23 February 2018 - 01:20 AM, said:

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";


i dont thing this would work if the file have space. for example 1|Ali Baba|2222
Was This Post Helpful? 0
  • +
  • -

#17 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 27 February 2018 - 06:32 PM

I think you should continue working on the code you posted in post #14. But it doesn't seem useful to read from the stringstream into a vector of strings. Instead, you should consider jimblumberg's suggestions regarding using a struct to record each line of data, with a separate field to hold each column's information.
Was This Post Helpful? 0
  • +
  • -

#18 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 01 March 2018 - 01:09 AM

i tried using struct but it seems that it only can hold 1 data at a time.

struct column
{
	string no;
	string id;
	string name;
};
int main()
{
	vector <string>collection = Open_File("file1.txt");
	vector <string> results;
	struct column column1;
	int i =1;
	
	while(i < collection.size()){
		column1.id = collection.at(i);
		i=i+3;
	}
	cout<<column1.id<<'\n';
	
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#19 jimblumberg   User is offline

  • member icon

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

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

Posted 01 March 2018 - 02:10 AM

Quote

i tried using struct but it seems that it only can hold 1 data at a time.


Have you considered a vector of this structure?

By the way you either need one more field in the structure or two structures, one for file one's data and one for file two's data.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2