Help with sorting a text file database

This is a discussion on Help with sorting a text file database within the C Programming forums, part of the General Programming Boards category; Hi, I've tried searching several resources for info on this but I still don't quite understand what I need to ...

From there, I am confused as to how to sort the data. My thoughts are to to make a bubble sort function that will take in each element of the struct in one at a time and sort it. Should I just write over the sorted array/struct, or store it in a new array? Can I sort the entire struct at once?

I can't find any good examples of passing struct array element things into another function. Do I need to use pointers for this?

Does this sound like the right idea, or am I going about it all wrong? I'm not concerned about sorting speed or anything, I just want it to work for now.

If you're going to use a struct, then consider making the members single datatypes and creating an array of objects. The way your doing it kind of defeats the purpose of the object. Of course your character still must be an array. Then all you need to do is make your sort routine comparing the int members, then swap the whole objects.

You.. can be a millionaire.. and never pay taxes! You can be a millionaire.. and never pay taxes! You say.. "Steve.. how can I be a millionaire.. and never pay taxes?" First.. get a million dollars.

Or rather, first find out the number of lines in a file. This must have been Googled several hundreds of thousands of times (finding the number of lines in a file), so searching ought to reveal more than one hit -- albeit probably 2/3 of which will contain questionable advice. [If you go line by line, life will probably suck a little bit more.]

But yes, you dynamically create an array of structs of however many lines of data you've got. Then you sort the data. And the C standard library supplies the function qsort -- all you need to do is tell qsort how to compare things -- which means you need to write a comparison function for it.

Anyways, this post does not provide a solution. It is merely here with a couple of key phrases mentioned for you to begin searching for one. This is not an attempt to be conceited, merely to help you learn to learn how to find the answer.

The hash table can easily be implemented using your structure,
get a function to scan each of the lines and store them in your struct array,then if u want to be sorting wrt the second element of each structure,just use the sortin algorithmn on the 2nd element of each structure,get them sorted and then once u got them sorted,print out the remaining elements of their corresponding structures in the same sequence.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell