(c) Sort Matrix of Structs

1

Good to all!

I have a problem that I can not solve and I seek help from members. In my code, I read the contents of a file and save it in a string for use in the code, in the main loop, I get this string and refer to the function call BreakStrungCompleta, to break the string "\ n" array of strings. Then it passes each line of this array to the function call BreakStruct, which will save a struct to the line separated by the delimiters ",", I will provide the example of the users.txt files, whoever is to test, can see that everything works well. The problem is that now I want to sort this array of structs, in the searches I did not find many examples and I look for help from the most experienced.

What I tried was the following: I create the comparison function that is forwarded to the qsort function call:

int comparar(const void * a, const void * b)
{

  const Cliente * ia = (Cliente *) a;
  const Cliente * ib = (Cliente *) b;
  return strcmp(ia->cpf, ib->cpf);

}

Then call the qsort function, step as the base pointer to my array of structs, and also compare function:

qsort(&cliente, 4, sizeof(Cliente), comparar);

printf("As struct's foram ordenadas\n");


for (int i = 0; i < 4; i++)
{

printf("%s\n", cliente[i]->cpf);

}

All compiles, the code runs after the qsort function call, but the error at the beginning, I believe the error is because, qsort expects an array of type;

example: Client * client, and my array and type Client *

#include<stdio.h>

#include<stdlib.h>

#include<string.h>



typedef struct Cliente
 {

    char cpf[13];
    char realName[60];
    char sexo[11];
    char key[15];
    char data[11];


}Cliente;






   char **QuebrarStringCompleta(char *string)
{

   char **linha;
   char *token;
   int tamVetor = 1;

   int i = 0;

   linha  = malloc(sizeof(char*)*tamVetor);

   linha[i] = malloc(sizeof(char**)*150);

   token = strtok(string, "\n");

   strcpy(linha[i], token);
   printf("primeira linha e: %s\n", linha[i]);

   i++;
   tamVetor++;


    while (token != NULL)
   {
      linha = realloc(linha, sizeof(char*)*tamVetor);

      linha[i] = malloc(sizeof(char**)*150);

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


        if (token == NULL)
         {
           printf("token e nulo: %s, break\n", token);
            break;
         }      
        else
          {
            strcpy(linha[i], token);
            i++;       
            tamVetor++;
          }
        }

    return linha;
  }



  Cliente* QuebrarStruct( char *string)
{


 Cliente *cliente;
 char *token;

    cliente = malloc(sizeof(Cliente));
    token = strtok(string,",");
    strcpy(cliente->cpf, token);
  token = strtok(NULL,",");
    strcpy(cliente->realName, token);
  token = strtok(NULL,",");
    strcpy(cliente->sexo, token);
    token = strtok(NULL,",");
    strcpy(cliente->data, token);

  return cliente;

}




char * ObterDados(int flag)
{



  FILE *users;  
    char url[] = "/bd/users/users/users.txt";       

if (flag == 1)
{   
 users = fopen(url, "r");

}

char *string;
    char ch;
     int i = 0;
      int tamVetor = 0;

      string = malloc(sizeof (char * ) * tamVetor); 

  if (users == NULL)
  {
    printf("Erro na abertura do arquivo!");
    return 1;
  }
  else
 {

     while ((ch = fgetc(users)) != EOF)
    {
        if (ch == '#')
        {

            while ((ch = fgetc(users)) != EOF)
            {


                string[i] = ch;

                                    i++;
                tamVetor++;

                string = realloc(string, sizeof(char *) * tamVetor);



                            }
                    }
             }


     }


            return string;

   }
  int comparar(const void * a, const void * b)
 {

  const Cliente * ia = (Cliente *) a;
  const Cliente * ib = (Cliente *) b;
  return strcmp(ia->cpf, ib->cpf);


}

int main ()

{

char *string;
char **result;

string = ObterDados(1);


printf("String e %s: ", string);

result = QuebrarStringCompleta(string);



printf("As linhas separadas sao: ");

for(int i = 0; i < 5 ; i++)
{

if(result[i] == NULL)
    break;
 printf("%s\n", result[i]);   

}



Cliente **cliente;

cliente= malloc(sizeof(Cliente)*5);
if (cliente == NULL) {
printf ("Socorro! malloc devolveu NULL!\n");
exit (EXIT_FAILURE);
}


for (int i = 0; i < 4; i++)
{
cliente[i] = QuebrarStruct(result[i]);

printf("Dados retornados e, usuario: %s, nome real: %s, sexo: %s, data: %s\n", cliente[i]->cpf,cliente[i]->realName, cliente[i]->sexo, cliente[i]->data);

}



qsort(&cliente, 4, sizeof(Cliente), comparar);

printf("As struct's foram ordenadas\n");


for (int i = 0; i < 4; i++)
{

printf("%s\n", cliente[i]->cpf);


}


}

I look forward to everyone's help!

File txt users.txt:

#,12092798611,Elizandro Silva,masculino,14/11/1995,
,12092798688,Aislan Silva,masculino,14/11/1995,
,12092798644,Maria Lourdes,feminino,27/01/1969,
,12092798600,Bob Marley,masculino,14/11/1995,
,12092798612,Elaine Silva,feminino,02/01/1992,
,12092798655,Jonas Machado,masculino,14/11/1995,
,12092798611,Mauro Silva,masculino,14/11/1995,
,12092798688,Bete Silva,masculino,14/11/1995,
,12092798644,Josimar Lourdes,feminino,27/01/1969,
    
asked by anonymous 01.07.2018 / 18:43

0 answers