Need help with C++ sorting file data

Discussion of chess software programming and technical issues.

Moderator: Ras

jhaglund2
Posts: 66
Joined: Mon Jan 16, 2017 6:28 pm

Need help with C++ sorting file data

Post by jhaglund2 »

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::vector<int> v, w, x, y, z;

if(data.is_open())
 {
     int i;
     int j;

    while (std::getline (data, line))
    {
        std::stringstream parse(line);
        
    	parse >> i;
        v.push_back(i);
				log[j] = i;
        cout << log[j]  << " ";
	    parse >> i;
        w.push_back(i);
        		log[j] = i;
        cout << log[j]  << " ";
     
        parse >> i;
        x.push_back (i);
        		log[j] = i;
        cout << log[j]  << " ";
     
        parse >> i;
        y.push_back (i);
        		log[j] = i;
        cout << log[j]  << " ";
    
        parse >> i;
        z.push_back (i);
        		log[j] = i;
        cout << log[j]  << " ";
       cout << endl;
         cout << "sorting ... " << endl;
	  cout << line << endl;  // display sort after.
	 }
  }     
} 

Or...

Code: Select all


int sort() {
	lines();
	string  m1st, m2nd, m3rd, m4th, m5th;
	ifstream readFile("data.txt");
	string line;
	int max = 5 * numLines;
        string parse[max];
	while(getline(readFile,line))   {
		stringstream iss(line);
		getline(iss, m1st, ' ');
		getline(iss, m2nd, ' ');
		getline(iss, m3rd, ' ');
		getline(iss, m4th, ' ');
		getline(iss, m5th);
	

iss >> ws;
if(iss == "  ") {
break;
}
 //before
		cout << " m1 " << m1st << " m2 " << m2nd << " m3 " << m3rd << " m4 " << m4th << " m5 " << m5th <<  endl;

  
cout << " " << endl;
for(int p = 0; p < numLines*5;){
cout << parse[p] << " ";
p++;
}


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

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


	cout << " m1 " << parse[1] << " m2 " << parse[2] << " m3 " << parse[3] << " m4 " << parse[4] << " m5 " << parse[5] <<  endl;
	cout << endl;
	}

	readFile.close();
	cout << "end of sort(); "<< endl;
}
Any help is welcome!
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: Need help with C++ sorting file data

Post by elcabesa »

I really don't understood what is the question.
AndrewGrant
Posts: 1959
Joined: Tue Apr 19, 2016 6:08 am
Location: U.S.A
Full name: Andrew Grant

Re: Need help with C++ sorting file data

Post by AndrewGrant »

This seems like more of a Stackoverflow question than a Talkchess question...
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Need help with C++ sorting file data

Post by AlvaroBegue »

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 " ", (sort {$a<=>$b} @F), "\n"'
jhaglund2
Posts: 66
Joined: Mon Jan 16, 2017 6:28 pm

Re: Need help with C++ sorting file data

Post by jhaglund2 »

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: 66
Joined: Mon Jan 16, 2017 6:28 pm

Re: Need help with C++ sorting file data

Post by jhaglund2 »

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: 66
Joined: Mon Jan 16, 2017 6:28 pm

Re: Need help with C++ sorting file data

Post by jhaglund2 »

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 " ", (sort {$a<=>$b} @F), "\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: 211
Joined: Sun Jan 18, 2009 11:27 pm
Location: Sweden
Full name: Patrik Karlsson

Re: Need help with C++ sorting file data

Post by elpapa »

Code: Select all

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

int main()
{
	std::ifstream data("data.txt");
	std::string line;
	std::vector<int> v;
	
	while (std::getline(data, line)) {

		v.clear();

		std::istringstream parse(line);
		int i;
		
		while (parse >> i)
			v.push_back(i);
		
		std::sort(v.begin(), v.end());

		for (int o : v)
			std::cout << o << " ";

		std::cout << std::endl;

	}

	std::cout << "Press Enter to exit";
	std::cin.get();

	return 0;
}
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Need help with C++ sorting file data

Post by AlvaroBegue »

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() {
  std::vector<int> v;
  std::string line;
  while (std::getline(std::cin, line)) {
    std::istringstream iss(line);
    v.clear();
    int i;
    while (iss >> i)
      v.push_back(i);
    std::sort(v.begin(), v.end());
    for (auto x : v)
      std::cout << x << ' ';
    std::cout << '\n';
  }
}

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: 211
Joined: Sun Jan 18, 2009 11:27 pm
Location: Sweden
Full name: Patrik Karlsson

Re: Need help with C++ sorting file data

Post by elpapa »

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.