Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
typedef enum{general, home, work, cell, FAX, other} Phone_type;
typedef struct
{
char *name;
char *number;
char *ext;
Phone_type type;
char *group;
void *future;
} Contact;
int my_cmp(Contact *s1, Contact *s2)
{
return(strcmp(s1->name,s2->name));
}
char * rtrim(char* str)
{
char * ptr;
ptr = strchr(str, 0) - 1;
while(ptr >= (char *) str && (isspace(*ptr)))
{
*ptr-- = '\0';
}
return str;
}
int main (int argc, char *argv[])
{
int len=0, i=0, length[6], count=0, tempnum=0;
char *line_ptr, one_line[25], temp[100], line[100];
FILE *input; FILE *config; FILE *output;
config=fopen(argv[1], "r");
for(i=1; i<=6; i++)
{
line_ptr=fgets(one_line, 64, config);
sscanf(one_line,"%d", &tempnum);
if(i==1)
length[0]=tempnum;
else if (i==2)
length[1]=tempnum;
else if (i==3)
length[2]=tempnum;
else if (i==4)
length[3]=tempnum;
else if (i==5)
length[4]=tempnum;
else if (i==6)
length[5]=tempnum;
}
input=fopen(argv[2], "r");
output=fopen(argv[3], "w");
for (count=0; 1;count++)
{
line_ptr=fgets(line,100,input);
if(line_ptr==NULL) break;
}
int Max_contacts=count;
Contact cell_list[Max_contacts];
freopen(argv[1], "r", input);
for (i=0; i<=count;i++)
{
line_ptr=fgets(line,100,input);
if(line_ptr==NULL) break;
strncpy(temp,line,length[0]);
temp[length[0]] = '\0' ;
len=strlen(rtrim(temp));
cell_list[i].name=calloc(len+1,sizeof(char));
strcpy(cell_list[i].name, temp);
strncpy(temp, line+length[0], length[1]);
temp[length[1]] = '\0';
len=strlen(rtrim(temp));
cell_list[i].number = calloc(len+1,sizeof(char));
strcpy(cell_list[i].number, temp);
strncpy(temp, line+length[0]+length[1], length[2]);
temp[length[2]] = '\0';
len=strlen(rtrim(temp));
cell_list[i].ext = calloc(len+1,sizeof(char));
strcpy(cell_list[i].ext, temp);
strncpy(temp, line+length[0]+length[1]+length[2], length[3]);
temp[length[3]] = '\0';
len=strlen(rtrim(temp));
cell_list[i].type = atoi(temp);
strncpy(temp, line+length[0]+length[1]+length[2]+length[3], length[4]);
temp[length[4]] = '\0';
len=strlen(rtrim(temp));
cell_list[i].group = calloc(len+1,sizeof(char));
strcpy(cell_list[i].group, temp);
strncpy(temp, line+length[0]+length[1]+length[2]+length[3]+length[4], length[5]);
temp[length[5]] = '\0';
len=strlen(rtrim(temp));
cell_list[i].future = (void*)calloc(len+1,sizeof(char));
strcpy(cell_list[i].future, temp);
}
printf("before sort1\n");
/* Sort by name */
qsort(cell_list, count, sizeof(Contact), my_cmp);
printf("before print1[%d]\n", count);
/* Printing values in xml format */
for (i=0;i<count;i++)
{
if(i>0){
if(strcmp(cell_list[i-1].name,cell_list[i].name)==0)
{
fprintf(output,"<record>\n");
fprintf(output,"<name>%s</name>\n", cell_list[i].name);
}
}
fprintf(output,"<number>%s</number>\n", cell_list[i].number);
fprintf(output,"<extension>%s</extension>\n", cell_list[i].ext);
fprintf(output,"<type>%d</type>\n", cell_list[i].type);
fprintf(output,"<group>%s</group>\n", cell_list[i].group);
fprintf(output,"<future>%s</future>\n", (char*)cell_list[i].future);
fprintf(output,"</record>\n");
}
fclose(output);
printf("after fclose");
return 0;
}