-1

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;
 }
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Quora Bai
  • 29
  • 4
  • Use a well-known sorting algorithm. [This one](https://www.geeksforgeeks.org/merge-sort/) should suffice. The C code there is perfectly serviceable; all you should have to do is replace the integer assignments with their `strcpy` equivalents. – Robert Harvey Oct 12 '20 at 18:53
  • Your outer `for` loop does not anything; it runs once. A general comparison sort algorithm necessarily has **two** nested loops while you have *0*... – Antti Haapala -- Слава Україні Oct 12 '20 at 18:54
  • Consider calling `qsort()`. – DYZ Oct 12 '20 at 18:55
  • ... which is defined in header file `` Documentation for `qsort()` is [here](https://en.cppreference.com/w/c/algorithm/qsort). – Robert Harvey Oct 12 '20 at 18:56
  • The question is very much like a series of questions such as this [deleted question](https://stackoverflow.com/questions/64301575/how-to-arrange-data-in-file-in-c) by a user whose name apppears in the data of this question. – Weather Vane Oct 12 '20 at 19:06

1 Answers1

0

You are overcomplicating things.

Try reading the contents of the file, and store the string of each line in a string array. Then sort the array, e.g. by using bubblesort.

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_LEN 200

void sort_strings(char arr[][STRING_LEN], int n);

int main(){
  char str[STRING_LEN], records[5][STRING_LEN];
  char * pch;
  FILE * fp1 = fopen("file.csv", "r");
  int count = 0;

  while(fgets(str, STRING_LEN, fp1))
  {
    str[strcspn(str, "\n")] = 0;
    pch = strtok(str, ",");
    strcpy(records[count++], pch);
  }
  printf("Before:\n");
  for(int i = 0; i < count; ++i)
    printf("%s\n", records[i]);
  sort_strings(records, count);
  printf("\nAfter:\n");
  for(int i = 0; i < count; ++i)
    printf("%s\n", records[i]);
 return 0;
}

void sort_strings(char arr[][STRING_LEN], int n)
{ 
    char temp[STRING_LEN]; 
  
    // Sorting strings using bubble sort 
    for (int j=0; j<n-1; j++) 
    { 
        for (int i=j+1; i<n; i++) 
        { 
            if (strcmp(arr[j], arr[i]) > 0) 
            { 
                strcpy(temp, arr[j]); 
                strcpy(arr[j], arr[i]); 
                strcpy(arr[i], temp); 
            } 
        } 
    } 
}

Output:

Before:
Bilal Khan
Ali Ahmed
hello
darth

After:
Ali Ahmed
Bilal Khan
darth
hello

I didn't use qsort() since I imagine you are doing this for practice.

Relevant links: Sort with bubblesort and Removing trailing newline character from fgets() input.


Edit with using qsort():

#include <stdio.h>
...
int compare(const void * a, const void * b);

int main(void)
{
  ...
  qsort(records, count, sizeof(records[0]), compare);
  printf("\nAfter:\n");
  for(int i = 0; i < count; ++i)
    printf("%s\n", records[i]);
 return 0;
}

int compare(const void * a, const void * b)
{
  return strcmp(a, b);
}

Output:

Before:
Bilal Khan
Ali Ahmed
hello
darth

After:
Ali Ahmed
Bilal Khan
darth
hello
gsamaras
  • 71,951
  • 46
  • 188
  • 305