| | |
sorting a file by specified fields
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
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.

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?
- 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?
•
•
Join Date: Oct 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
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.
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.
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define NELEMS 4 struct crap { char word[200]; int word_length; }; static int name_comp(const void *, const void *); static int word_length_comp(const void *, const void *); int main() { size_t i; static struct crap stuff[NELEMS] = {{"yo",2}, {"momma",5}, {"iz",2}, {"phat",4}}; qsort(stuff, NELEMS, sizeof stuff[0], name_comp); puts("By alphabetical order:"); for (i = 0; i < NELEMS; ++i) printf("%s, %d\n", stuff[i].word, stuff[i].word_length); qsort(stuff, NELEMS, sizeof stuff[0], word_length_comp); puts("\nBy word length:"); for (i = 0; i < NELEMS; ++i) printf("%s, %d\n", stuff[i].word, stuff[i].word_length); getchar(); getchar(); return 0; } static int name_comp(const void *p1, const void *p2) { struct crap *sp1 = (struct crap *) p1; struct crap *sp2 = (struct crap *) p2; int order = strcmp(sp1->word,sp2->word); return order; } static int word_length_comp(const void *p1, const void *p2) { struct crap *sp1 = (struct crap *) p1; struct crap *sp2 = (struct crap *) p2; return sp1->word_length - sp2->word_length; }
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*
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: break an integer into its component
- Next Thread: Reverse string arrays
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks binarysearch calculate changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking hardware histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






