Need help with C++ sorting file data

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
jhaglund2
Posts: 46
Joined: Mon Jan 16, 2017 5:28 pm

Need help with C++ sorting file data

Post by jhaglund2 » Sun Jul 02, 2017 5:50 am

I'm looking for some help with sorting information taken from a file.

Code: Select all

Example: data.txt

10 3 41 12 1
23 12 34 22 3
32 3 30 2 12

output:

1 3 10 12 41
3 12 22 23 34
2 3 12 30 32

Code: Select all

int parse()
{
	
	lines(); 
	int max = 5 * numLines;
    std::string line;
	int log[max];	 
	ifstream data ("data.txt");
	
	std&#58;&#58;vector<int> v, w, x, y, z;

if&#40;data.is_open&#40;))
 &#123;
     int i;
     int j;

    while &#40;std&#58;&#58;getline &#40;data, line&#41;)
    &#123;
        std&#58;&#58;stringstream parse&#40;line&#41;;
        
    	parse >> i;
        v.push_back&#40;i&#41;;
				log&#91;j&#93; = i;
        cout << log&#91;j&#93;  << " ";
	    parse >> i;
        w.push_back&#40;i&#41;;
        		log&#91;j&#93; = i;
        cout << log&#91;j&#93;  << " ";
     
        parse >> i;
        x.push_back &#40;i&#41;;
        		log&#91;j&#93; = i;
        cout << log&#91;j&#93;  << " ";
     
        parse >> i;
        y.push_back &#40;i&#41;;
        		log&#91;j&#93; = i;
        cout << log&#91;j&#93;  << " ";
    
        parse >> i;
        z.push_back &#40;i&#41;;
        		log&#91;j&#93; = i;
        cout << log&#91;j&#93;  << " ";
       cout << endl;
         cout << "sorting ... " << endl;
	  cout << line << endl;  // display sort after.
	 &#125;
  &#125;     
&#125; 

Or...

Code: Select all


int sort&#40;) &#123;
	lines&#40;);
	string  m1st, m2nd, m3rd, m4th, m5th;
	ifstream readFile&#40;"data.txt");
	string line;
	int max = 5 * numLines;
        string parse&#91;max&#93;;
	while&#40;getline&#40;readFile,line&#41;)   &#123;
		stringstream iss&#40;line&#41;;
		getline&#40;iss, m1st, ' ');
		getline&#40;iss, m2nd, ' ');
		getline&#40;iss, m3rd, ' ');
		getline&#40;iss, m4th, ' ');
		getline&#40;iss, m5th&#41;;
	

iss >> ws;
if&#40;iss == "  ") &#123;
break;
&#125;
 //before
		cout << " m1 " << m1st << " m2 " << m2nd << " m3 " << m3rd << " m4 " << m4th << " m5 " << m5th <<  endl;

  
cout << " " << endl;
for&#40;int p = 0; p < numLines*5;)&#123;
cout << parse&#91;p&#93; << " ";
p++;
&#125;


//after
cout << "sorting ... " << endl; 

cout << endl;	
	cout <<  " m1 " << m1st << " m2 " << m2nd << " m3 " << m3rd << " m4 " << m4th << " m5 " << m5th <<  endl;


	cout << " m1 " << parse&#91;1&#93; << " m2 " << parse&#91;2&#93; << " m3 " << parse&#91;3&#93; << " m4 " << parse&#91;4&#93; << " m5 " << parse&#91;5&#93; <<  endl;
	cout << endl;
	&#125;

	readFile.close&#40;);
	cout << "end of sort&#40;); "<< endl;
&#125;
Any help is welcome!

elcabesa
Posts: 830
Joined: Sun May 23, 2010 11:32 am
Contact:

Re: Need help with C++ sorting file data

Post by elcabesa » Sun Jul 02, 2017 6:21 am

I really don't understood what is the question.

AndrewGrant
Posts: 584
Joined: Tue Apr 19, 2016 4:08 am
Location: U.S.A
Full name: Andrew Grant
Contact:

Re: Need help with C++ sorting file data

Post by AndrewGrant » Sun Jul 02, 2017 6:31 am

This seems like more of a Stackoverflow question than a Talkchess question...

AlvaroBegue
Posts: 925
Joined: Tue Mar 09, 2010 2:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Need help with C++ sorting file data

Post by AlvaroBegue » Sun Jul 02, 2017 6:44 am

AndrewGrant wrote:This seems like more of a Stackoverflow question than a Talkchess question...
It looks more like a homework question than anything else, actually.

In Perl:

Code: Select all

perl -ane 'print join " ", &#40;sort &#123;$a<=>$b&#125; @F&#41;, "\n"'

jhaglund2
Posts: 46
Joined: Mon Jan 16, 2017 5:28 pm

Re: Need help with C++ sorting file data

Post by jhaglund2 » Sun Jul 02, 2017 6:51 am

elcabesa wrote:I really don't understood what is the question.
Need to sort each line in the text file from smallest to largest. The above is what I have so far.

jhaglund2
Posts: 46
Joined: Mon Jan 16, 2017 5:28 pm

Re: Need help with C++ sorting file data

Post by jhaglund2 » Sun Jul 02, 2017 6:53 am

AndrewGrant wrote:This seems like more of a Stackoverflow question than a Talkchess question...
Thanks for the suggestion. I will look into that site.

jhaglund2
Posts: 46
Joined: Mon Jan 16, 2017 5:28 pm

Re: Need help with C++ sorting file data

Post by jhaglund2 » Sun Jul 02, 2017 6:58 am

AlvaroBegue wrote:
AndrewGrant wrote:This seems like more of a Stackoverflow question than a Talkchess question...
It looks more like a homework question than anything else, actually.

In Perl:

Code: Select all

perl -ane 'print join " ", &#40;sort &#123;$a<=>$b&#125; @F&#41;, "\n"'
No, not homework, just a personal project that I've been stuck on for years... and I finally came back to it. Multi-purpose, like most things chess or not... Thanks for trying.

elpapa
Posts: 203
Joined: Sun Jan 18, 2009 10:27 pm
Location: Sweden
Full name: Patrik Karlsson

Re: Need help with C++ sorting file data

Post by elpapa » Sun Jul 02, 2017 1:00 pm

Code: Select all

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

int main&#40;)
&#123;
	std&#58;&#58;ifstream data&#40;"data.txt");
	std&#58;&#58;string line;
	std&#58;&#58;vector<int> v;
	
	while &#40;std&#58;&#58;getline&#40;data, line&#41;) &#123;

		v.clear&#40;);

		std&#58;&#58;istringstream parse&#40;line&#41;;
		int i;
		
		while &#40;parse >> i&#41;
			v.push_back&#40;i&#41;;
		
		std&#58;&#58;sort&#40;v.begin&#40;), v.end&#40;));

		for &#40;int o &#58; v&#41;
			std&#58;&#58;cout << o << " ";

		std&#58;&#58;cout << std&#58;&#58;endl;

	&#125;

	std&#58;&#58;cout << "Press Enter to exit";
	std&#58;&#58;cin.get&#40;);

	return 0;
&#125;

AlvaroBegue
Posts: 925
Joined: Tue Mar 09, 2010 2:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Need help with C++ sorting file data

Post by AlvaroBegue » Sun Jul 02, 2017 1:03 pm

I wrote this last night, before seeing Patrik's code. Apparently it's a very natural way to do it.

Code: Select all

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

int main&#40;) &#123;
  std&#58;&#58;vector<int> v;
  std&#58;&#58;string line;
  while &#40;std&#58;&#58;getline&#40;std&#58;&#58;cin, line&#41;) &#123;
    std&#58;&#58;istringstream iss&#40;line&#41;;
    v.clear&#40;);
    int i;
    while &#40;iss >> i&#41;
      v.push_back&#40;i&#41;;
    std&#58;&#58;sort&#40;v.begin&#40;), v.end&#40;));
    for &#40;auto x &#58; v&#41;
      std&#58;&#58;cout << x << ' ';
    std&#58;&#58;cout << '\n';
  &#125;
&#125;

EDIT: Note that even most of the variables have the same names!!! The biggest difference between the two programs seems to be that I work in a Unix style --where it's natural to read from standard input and write to standard output-- and he probably works on Windows.

elpapa
Posts: 203
Joined: Sun Jan 18, 2009 10:27 pm
Location: Sweden
Full name: Patrik Karlsson

Re: Need help with C++ sorting file data

Post by elpapa » Sun Jul 02, 2017 2:02 pm

AlvaroBegue wrote:The biggest difference between the two programs seems to be that I work in a Unix style --where it's natural to read from standard input and write to standard output-- and he probably works on Windows.
Great minds think alike :wink:

And yes, I primarily work on Windows.

Post Reply