Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 31st, 2008, 1:43 PM   #1
jack0987
Newbie
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0 jack0987 is on a distinguished road
C++ file sorting

Can somebody give me an idea how to start doing this??

Tasks:
1. There is one input file which contains a list of data with a number of columns and rows.
2. The file should be sorted according to different column(ranks, location...) depending on the input of the user.


My problem is that how should i read the rows so that it can be stored it in a structure, such that the file can be sorted according to one column at a time(e.g. according to the rank).

Should spaces be taken into consideration while reading the file??

thanks in advance for any help would be appreciated!
jack0987 is offline   Reply With Quote
Old Mar 31st, 2008, 2:17 PM   #2
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: C++ file sorting

Perhaps you could read the file line by line, and then split the string and store each value into and array, this way you can then access each value and say store it into an array for each coloumn. Checking for white space is a good way to know where to split your string. This may be helpful to you as to how you can cut the string. Also combine this with white space checking and you should have a solution to your problem.

Chris
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old Mar 31st, 2008, 9:00 PM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Re: C++ file sorting

Like Chris said, you basically need to decide on a delimiting character to separate your columns. You could use a comma, which is fairly common. This of course means that none of your values can contain a comma (or whatever character you choose) unless you provide another marker to escape that value, which then makes the marker invalid unless you have a sequence to add it (think of how escaping with \ works; you can probably just use that, really).
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 1st, 2008, 3:25 PM   #4
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,011
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: C++ file sorting

How is the file laid out? Are the columns padded with spaces or tabs to line up nicely for a human reader, or is there some other delimiter used? Is the file even plain text, or is it a binary file made up of fixed-size records?

If it's a plain text file, perhaps you could post a small piece (half a dozen lines or so) of it here inside [code] [/code] tags to preserve the formatting, and we can offer more helpful suggestions.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Apr 1st, 2008, 3:57 PM   #5
jack0987
Newbie
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0 jack0987 is on a distinguished road
Re: C++ file sorting

thanks again!!
here is how the file is laid out
 World Rank	Institution	             			Region	Regional Rank	Country		National Rank	Score on Alumni	Score on Award	Score on HiCi	Score on N&S	Score on SCI	Score on Size	Total Score
1	        Harvard Univ	            			Americas	1	USA			1		100		100	 	100		100		100		72.4		100
2	       Univ Cambridge	             			Europe		1	UK			1		99.8		93.4		53.3		56.6		70.9		66.9		73.6
3	       Stanford Univ	            			Americas	2	USA			2		41.1		72.2		88.5		70.9		72.3		65		73.4
4	      Univ California - Berkeley    			Americas	3	USA			3		71.8		76		69.4		73.9		72.2		52.7		72.8
5	    Massachusetts Inst Tech (MIT)  			Americas	4	USA			4		74		80.6		66.7		65.8		64.3		53		70.1
6	      California Inst Tech	   			Americas	5	USA			5		59.2		68.6		59.8		65.8		52.5		100		67.1
7	         Columbia Univ         				Americas	6	USA			6		79.4		60.6		56.1		54.2		69.5		45.4		62.3
8	          Princeton Univ				Americas	7	USA			7		63.4		76.8		60.9		48.7		48.5		59.1		60.9
9	            Univ Chicago				Americas	8	USA			8		75.6		81.9		50.3		44.7		56.4		42.2		60.1
10	            Univ Oxford	         			Europe	        2	UK			2		64.3		59.1		48.4		55.6		68.4		53.2		59.7
11	             Yale Univ  				Americas	9	USA			9		52.1		44.5		60.3		57.2		63.9		49.3		56.9
12	            Cornell Univ				Americas	10	USA			10		46.5		52.4		55		48.8		66.3		39.8		54.6
13	Univ California - San Diego				Americas	11	USA			11		17.7		34.7		59.8		56.5		64.5		46.6		51
jack0987 is offline   Reply With Quote
Old Apr 3rd, 2008, 2:14 AM   #6
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: C++ file sorting

here is a start. it copies standard input into a vector, sorts it, then copies the vector to standard output. this just uses a lexicographical string comparison.
c++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. #include <vector>
  3. #include <iterator>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. int main()
  8. {
  9. std::vector< std::string > v;
  10. std::copy(std::istream_iterator< std::string >(std::cin), std::istream_iterator< std::string >(), std::back_insert_iterator< std::vector <std::string > >(v));
  11. std::sort(v.begin(), v.end());
  12. std::copy(v.begin(), v.end(), std::ostream_iterator< std::string >(std::cout, "\n"));
  13. return 0;
  14. }
a good place for you to begin would to be overriding the < operator for std::string.
mbd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
Sorting By File Size Steiner Visual Basic 3 Jun 17th, 2006 1:58 PM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM
Structure char field to a disk file ehab_aziz2001 C++ 0 Feb 10th, 2005 2:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC