Read all the lines into a container in your program. Sort the container. Then write the contents of the container back to your file, overwriting the previous unsorted contents of the file. What container you use is up to you based on your knowledge and experience. What sorting algorhythm you use is up to you, too, but choices include bubble sort, quick sort, insertion sort, etc.
A Word file probably has a lot of extraneuous gunk in it to make it pretty when you open the file in Word. If you can save the contents in a plain text file, like you would get with Note Pad, that might be easier to read from, unless you know the specific set up of the Word file. Of course, if you have no trouble reading in the Word file, then you don't have to worry about this paragraph.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
P.S. I'm asking because I want to arange my list of music albums, because right now, the names are held in a completely sporadic manner in a Word document.:cheesy:
Copy and paste the contents of the word file in a clean text file to avoid unnecessary hassles. I dont think there would be a requirement of your course to read specificallly from a *word file* .
Word File is not a text editor but used to present text and such tools are not used for storing data. Data files of user either have a header format which the user has designated (like a game's save file) and he knows or they are just pure text with custom extension (names.sdc) .This might sound stupid...but what is a container? Either it's an English word for something I'm familiar with (English isn't my first language) or I simply don't know what it is.
And please name some containers.
Simply put a container is something which is used to store loads of data or records and which can be accssed in O (1) time if the index of that element is know.
THe basic containers are well -- arrays :D in C and Vectors in C++.
If you using arrays you can use the inbuilt qsort( ) method to sort the array and if you usign vectors you can use the inbuilt sort( ) algorithm.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
>>but what is a container?
an array, std::vector, std::list, etc.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Dont use direct assignment operator. You can't treat strings like normal data types in terms of assignment.
You need to use strcpy to do somthing like that.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Study the links which I have given you, my writing the code wont help you in any way.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
>This isn't even part of my course, I just want to sort my music.
I don't believe you. I think this is homework and you are trying to trick me.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I wouldn't use strlen as a variable since it is an actual function.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Hmm looks like the code sample is broken.
Try this:
int compare_ints( const void* a, const void* b ) {
char* arg1 = (char*) a;
char* arg2 = (char*) b;
return strcmp( arg1, arg2 ) ;
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
starr expects a 2d array
What do you think this is...
strcpy(starr[i],s);
Is that a 2d array?
No offence but I'd scrap that and try again. SOS has given you a crappy example ha ha.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
fscanf( ) accpets the addr of the variable where the data is to be stored.
What you have is:
while(fscanf(a,"%s",&s)!=EOF)
Try replacing &s by &s[0] or s and then see.
No offence but I'd scrap that and try again. SOS has given you a crappy example ha ha.
If you cant help atleast keep your bad sense of humour to yourself.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734