I'm about to make code that read the text file line by line, then store it to stucture, then use quicksort. But my code doesn't print anything. Here is my code and text file.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

struct city{
    char nation[2];
    char region[10];
    char name[50];
    int population;
    double latitude;
    double longitude;
};

struct city p1[100];

int compare_with_name(const void *a, const void  *b)
{
    struct city* ptr_a = (struct city*)a;
    struct city* ptr_b = (struct city*)b;

    if (ptr_a->name < ptr_b->name) return 1;
    else if (ptr_a->name == ptr_b->name) return 0;
    else return -1;
}

int compare_with_population(const void *a, const void  *b)
{
    struct city* ptr_a = (struct city*)a;
    struct city* ptr_b = (struct city*)b;

    if (ptr_a->population < ptr_b->population) return 1;
    else if (ptr_a->population == ptr_b->population) return 0;
    else return -1;
}

int compare_with_latitude(const void *a, const void  *b)
{
    struct city* ptr_a = (struct city*)a;
    struct city* ptr_b = (struct city*)b;

    if (ptr_a->latitude < ptr_b->latitude) return 1;
    else if (ptr_a->latitude == ptr_b->latitude) return 0;
    else return -1;
}
void split() {
    FILE* fp;
      fp = fopen("world_cities.txt", "r");
      char buff[255];
      char **a=(char**)malloc(20000000 * sizeof(char*));
      int i=0;
      char* tok;
      char* split;

      while(!feof(fp)){
          fgets(buff,255,fp);

          tok=strtok(buff,"\n");

          split = (char* )malloc(strlen(tok) * sizeof(char));
          strcpy(split, tok);
          a[i]=split;
          i++;
      }

      while (tok != NULL) {

            tok = strtok(NULL, ",\n");

            if (tok == NULL) {
              break;
            }
            split = (char* )malloc(strlen(tok) * sizeof(char));
            strcpy(split, tok);

            a[i] = split;
            i += 1;
          }

      for(int k=0;k<47913;k++){
          scanf("%s %s %s %d %lf %lf",p1[k].nation,p1[k].region,p1[k].name,&p1[k].population,&p1[k].longitude,&p1[k].latitude);
}

       fclose(fp);
}

int main(){
       split();
       qsort(p1,100,sizeof(struct city),compare_with_name);

       for(int i=0;i<47913;i++){
           printf("%s %d %lf %lf",p1[i].name, p1[i].population,p1[i].longitude,p1[i].latitude);
       }

}

Why doesn't void split() have a return statement?

I didn't know that void split() needs return statement.. Could you tell me what should I add?

I get the feeling you want to debate if a return statement is required. This is a well discussed area and I will not debate this. If this was submitted as a class assignment you would be marked down a grade level. Why?

But in the end, flowing off the end of a non-void function still is undefined behavior. It might work on certain compilers, but it is not and never was guaranteed to. - https://stackoverflow.com/questions/32513793/c-and-c-functions-without-a-return-statement

So my question to you is why there is no return there. You need to be ready to answer that to your boss or professor.

But in the end, flowing off the end of a non-void function still is undefined behavior.

Correct. But is that relevant here in a void function like th OP's split function?

void split()

To the OP, a comment or two in the code would be helpful. In particular, I see in line 50 an allocation of 80,000,000 contiguous bytes of memory (assuming a pointer is 4 bytes). That's a lot of contiguous memory. Why so much? A comment is in order and perhaps a named constant in place of 20,000,000. Ditto the 47,913 on line 80. I also get a little nervous when I see 80,000,000 bytes of memory allocated with malloc and I don't see the word free anywhere.

And I get REALLY nervous when I see lines 73 and 74. You appear to be using strcpy and perhaps not allocating room for that pesky NULL terminator?

I also see a loop where k can be anything less than 47,913 and is used an array index for an array with 100 elements. What happens when k is greater than or equal to 100?

Also, formatting, formatting, formatting. I like to see constant indentation of brackets. When I see an ending bracket on line 82, I want to be able to find the starting bracket without any trouble at all simply by the indentation. It looks like the end to a function, but it isn't.

The entire program needs a complete rewrite.

commented: Yes. Needs a rewrite. +14
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.