I have read in a file, and i can print it out successfully, but i must sort the pointer to the array of structures. below is my code. I must sort the phone book in ascending order by zip code. If anyone could please help that would be great.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 typedef struct{
6 char name[50];
7 char street[50];
8 char city_state[50];
9 int zip;
10 } ADDRESS;
11
12 void sort(ADDRESS *a, int * i);
13 ADDRESS* readin(ADDRESS *a, int * i, char *pathin);
14 void print(ADDRESS *a, int * i, char *pathout);
15 int compare(const void *p1, const void *p2);
16 int main(int argc, char * argv[])
17 {
18 int *i;
19
20 if(argc == 3){
21 ADDRESS*a = readin(a, i, argv[1]);
22 sort(a,i);
23 print(a, i, argv[2]);
24 }else{
25 fprintf(stderr, "Usage: %s <inputfilename> <outputfilename>\n",
26 argv[0]);
27 exit(1);
28 }
29
30 return 0;
31 }
32 ADDRESS *readin (ADDRESS *a, int *i,char *pathin)
33 {
34 char buffer[10000];
35 char temp_zip[50];
36 char *line;
37 int count=0;
38 FILE *fp;
39 fp = fopen(pathin, "r");
40 *i = 0;
41 a = (ADDRESS *)malloc(sizeof(ADDRESS)*50);
42
43
44 while (!feof(fp) && (*i < 49))
45 {
46
47 line = fgets(buffer, 1000, fp);
48 if(line == NULL)
:set number 1,1 Top
92 fputs("\n", fq);
93 c++;
94 a++;
95 }
96 fclose(fq);
97 }
98 void sort(ADDRESS *a, int *i)
99 {
100 qsort(a, *i, sizeof(ADDRESS),compare);
101 }
102
103
104 int compare(const void *p1, const void *p2){
105 const struct ADDRESS *a1 = p1;
106 const struct ADDRESS *a2 = p2;
107 if(a1->zip > a2->zip){
108 return 1;
109 }else{
110 return -1;
111 }
112 return 0;
113 }