how to sort the data present in file in ascending or descending order

S

sudhir

hi

My application receives daily records, I need to show it sorted if
needed...but I am not finding the right direction to do that...
 
S

Simon Biber

sudhir said:
hi

My application receives daily records, I need to show it sorted if
needed...but I am not finding the right direction to do that...

Here's the basic approach...

a) Define a struct type to represent a record of the file

b) Read the file and split each record into the correct struct fields,
storing it in an array (or other data structure) of the structs.

c) Write a comparison function that determines an ordering between two
given records, possibly based on a configurable choice of column
priorities. The function prototype should be
int foo(const void *a, const void *b);
where it takes pointers to the data, and returns -1, 0, or 1 to
signify less, equal or greater respectively.

d) Sort the data using the comparison function. If you chose an array,
you can just use the standard qsort function here.

Let us know if you're having trouble with any of those points, post a
complete and compilable example, and we will be happy to help you further.
 
S

sudhir

I used c++ and I dare of pointers.. I used classes so I tried it much
better. I sorted the one file using bubble sort but it takes 3
temporary unnecessary files to do so. can u have any better idea.
Thanks
 
P

pete

sudhir said:
hi

My application receives daily records, I need to show it sorted if
needed...but I am not finding the right direction to do that...

The last time I sorted a file:
I read the whole thing into a linked list.
I sorted the list.
I overwrote the file.
 
S

Simon Biber

sudhir said:
I used c++ and I dare of pointers.. I used classes so I tried it much
better. I sorted the one file using bubble sort but it takes 3
temporary unnecessary files to do so. can u have any better idea.
Thanks

Look up the word 'dare'. It does not mean what you think it means.

If you are using C++, then you should be posting in comp.lang.c++

We only discuss C here.

If you would like to implement this in C, then you will be using a
'struct' type instead of a 'class' type. You will find they are similar,
except that they are always public, there is no inheritance, no
constructors or destructors, and no member functions.

I don't understand why you needed any temporary files just to read a
file in, sort it, and write it back out.
 
K

Kenny McCormack

Look up the word 'dare'. It does not mean what you think it means.

I assume it's another bit of "Indian English". Probably means "am afraid
of" or something like that.

To the OP: Also, while you are at it, look up "dorky 3rd worlder".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top