sorting a file by specified fields

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 3
Reputation: lynxul is an unknown quantity at this point 
Solved Threads: 0
lynxul lynxul is offline Offline
Newbie Poster

sorting a file by specified fields

 
0
  #1
Oct 29th, 2006
Hello Everyone!

I am due with an assignment that I just can't seem to acomplish. So here's the deal: I get a file with the following structure:

TyP_1 TyP_C2 TyP_C3
ORDER
Nr_rec
rec1_field1 rec1_field2 rec1_field3
rec2_field1 rec2_field2 rec2_field3



Typ_x can be S for string N for numeric or C for character
Order is a numeric (1,2,3) by which the sorting will take place
Nr_rec is the number of data lines below Nr_rec



What I have to do is sort them acording to these specifications,and output to out.txt.



Any help would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: sorting a file by specified fields

 
0
  #2
Oct 29th, 2006
Well, have you managed to get as far as

- open the input file
- read all the data into a suitable data structure
- close the input file
Then
- open the output file
- write all the data from your structure to the file
- close the output file.

Being able to read the file in, and write it back out again unchanged is an important first step. The sort just isn't going to happen until that's done.

Are you using C or C++?
Are you allowed to use one of the standard sort functions, or do you have to write your own?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: lynxul is an unknown quantity at this point 
Solved Threads: 0
lynxul lynxul is offline Offline
Newbie Poster

Re: sorting a file by specified fields

 
0
  #3
Oct 29th, 2006
yes, I have opened the file and have stored the values in two arrays, one is made up of the init variables, like type, number of records, etc, and the other one is for the data itself.

it would be best if I would write my own sort routine, but it is not mandatory.

I can output to a file easily, as soon as I sort them

C or C++, they're both ok.
Last edited by lynxul; Oct 29th, 2006 at 6:45 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: sorting a file by specified fields

 
0
  #4
Oct 29th, 2006
>C or C++, they're both ok.

So pick one. Then come back when you've decided otherwise you're going to get a very varied and confusing replies.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: lynxul is an unknown quantity at this point 
Solved Threads: 0
lynxul lynxul is offline Offline
Newbie Poster

Re: sorting a file by specified fields

 
0
  #5
Oct 29th, 2006
Ok. I will use C then. any ideas on structure or examples of code will be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: sorting a file by specified fields

 
0
  #6
Oct 29th, 2006
Lookup the qsort() function declared in stdlib.h

All you need to do is write a compare function to compare two elements of you array.

If your array has a type T, then the compare function receives two T* parameters (as const void*), which you then cast back to T*.
You then dereference and compare the data to implement your sort ordering.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: sorting a file by specified fields

 
0
  #7
Oct 29th, 2006
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define NELEMS 4
  6.  
  7. struct crap
  8. {
  9. char word[200];
  10. int word_length;
  11. };
  12.  
  13. static int name_comp(const void *, const void *);
  14. static int word_length_comp(const void *, const void *);
  15.  
  16. int main()
  17. {
  18. size_t i;
  19. static struct crap stuff[NELEMS] =
  20. {{"yo",2},
  21. {"momma",5},
  22. {"iz",2},
  23. {"phat",4}};
  24.  
  25. qsort(stuff, NELEMS, sizeof stuff[0],
  26. name_comp);
  27.  
  28. puts("By alphabetical order:");
  29. for (i = 0; i < NELEMS; ++i)
  30. printf("%s, %d\n",
  31. stuff[i].word,
  32. stuff[i].word_length);
  33.  
  34. qsort(stuff, NELEMS, sizeof stuff[0],
  35. word_length_comp);
  36.  
  37. puts("\nBy word length:");
  38. for (i = 0; i < NELEMS; ++i)
  39. printf("%s, %d\n",
  40. stuff[i].word,
  41. stuff[i].word_length);
  42.  
  43. getchar();
  44. getchar();
  45. return 0;
  46. }
  47.  
  48. static int name_comp(const void *p1, const void *p2)
  49. {
  50. struct crap *sp1 = (struct crap *) p1;
  51. struct crap *sp2 = (struct crap *) p2;
  52. int order = strcmp(sp1->word,sp2->word);
  53.  
  54.  
  55. return order;
  56. }
  57.  
  58. static int word_length_comp(const void *p1, const void *p2)
  59. {
  60. struct crap *sp1 = (struct crap *) p1;
  61. struct crap *sp2 = (struct crap *) p2;
  62.  
  63. return sp1->word_length - sp2->word_length;
  64. }

How about this. Two of the sorting parts are done for you, eg. sort by string and sort by number. All you have to do is define one more function to sort by a single character.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC