(c) Sort Struct Matrix

Asked

Viewed 153 times

1

Good to all!

I have a little problem that I can’t solve and I seek help from the members. In my code, I read the contents of a file and save in a string for use in the code, in the main loop, I receive this string and pass to the call of the function Quebrarstrungcompleta, to break the string by " n" and save in an array of strings. After passing each row of this matrix to the call of the function Quebrarstruct, which will store in a struct the line separated by the delimiters ",", I will provide the example of the users.txt files, who will test, can see that even everything works well. The problem is that now I want to order this array of structs, in the researches I did not find many examples and I seek help from the most experienced.

What I tried was the following: I create the comparison function that is passed 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 function qsort, step as base the pointer to my structs array, and also the 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);

}

Everything compiles, the code runs after the call of the qsort function, but from the error when printing, I think the error is because, qsort expects an array of type;

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

#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 await the help of all!

txt file 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,
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.