![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2008
Posts: 2
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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: ')
|
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
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> |
|
|
|
|
|
#4 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,011
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2008
Posts: 2
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
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)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |