In this code, the data is not arranged after swaping. It is giving me the same result as it is text in file. This means without any alphabetical order.
Text in file
Bilal Khan,1111111111111
Ali Ahmed,2222222222222
hello,3333333333333
darth,4444444444444
Expected output
Arrangement of data in alphabetical order.
Result showing
Without any change the data from the file appearing the same.
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_LEN 200
int main(){
char string[STRING_LEN], first[5][20], s[10];
FILE* fp1 = fopen("file.csv", "r");
char* data;
int i = 0, round, number=0, r;
for(number=0; ; number+=1){
while(fgets(string, STRING_LEN, fp1)){
data = strtok(string, ",");
if(i == number){
// printf("%s\n", data);
strcpy(first[number], data);
}
i++;
number++;
}
r = strcmp(first[number], first[number+1]);
if(r>0){
strcpy(s, first[number]);
strcpy(first[number], first[number+1]);
strcpy(first[number+1], s);
}
printf("\nSorted array\n");
for(i=0; i<=number; i++){
puts(first[i]);
}
break;
}