#include<stdio.h>
#include<stdlib.h>
#include<string.h>
static int compare(const void *x,const void *y){
return strcmp(*(const char**)x, *(const char**)y);
}
int main(){
FILE *p = fopen("file.txt","w");
char ch = '\0',**c = (char**)calloc(6,sizeof(char*));
int n[6]={0},i=0,j=0;
fprintf(p,"jack\nakki\njohn\nrachael\nrobin\ntom");
fclose(p);
p = fopen("file.txt","r");
while(1){
while((ch=getc(p))!= '\n'){
if(ch == EOF) break;
putchar(ch);
n[i]++;
}
printf(" %d\n",n[i]);
if(ch == EOF) break;
ch = '\0';
i++;
}
ch = '\0';
for(i=0;i<6;i++)
c[i] = (char*) calloc(n[i],sizeof(char));
fclose(p);
i=0;
p = fopen("file.txt","r");
while(1){
while((ch=getc(p))!= '\n'){
if(ch == EOF) break;
*(c[i]+j) = ch;
j++;
}
i++;
j = 0;
if(ch == EOF) break;
ch = '\0';
}
qsort(*c,6,sizeof(char*),compare);// why won't this work?
for(i=0;i<6;i++)
printf("%s\n",*c[i]);
return 0;
}
Akhilesh_2 0 Newbie Poster
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
Akhilesh_2 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.