problem with reading files / sorting data in c

1

Hello, my problem is the following , in the topic created by Rafael.

link

About problem reading strings.

String reading problem

You asked about ordering the t-shirts in c and then had the reply from Rafael Bluhn.

link

But I want to use with files reading a "file.txt" that will contain the data needed to pass the values to the structure that is in Rafael's code thus sorting name, color, size and then saving the changed data in another "file.txt, then print the ordered data on the screen.

I have the code part to open the file but I can not gather the information (which starts with an integer after strings) to move to Raphael's structure.

code


#include &ltstdio.h&gt
#include &ltstdlib.h&gt
#include &ltstring.h&gt

void removeNovaLinha( char *str );

int main( int argc, char *argv[] )
{
  FILE *fp;
  int aux, tamanho;
  char buffer[ 256 ];
  char **listaNomes;
  char *nome;

  fp = fopen( "documento.txt", "r" );//digitar o nome do documento que esta na mesma pasta que o programa .c
  //o arquivo deverá conter o "documento.txt" com os dados criados pelo Rafael
  if ( fp == NULL ) {
    printf( "Erro: nao posso abrir o arquivo %s!\n","documento.txt"  );

    exit( EXIT_FAILURE );
  }
  if ( !fscanf( fp, "%d\n", &tamanho ) ) {
    printf( "Erro: O arquivo deve comecar com um num. inteiro\n" );

    exit( EXIT_FAILURE );
  }

  listaNomes = calloc( tamanho, sizeof( char * ) );
  if ( listaNomes == NULL ) {
    printf( "Erro: nao posso alocar memoria!\n" );

    exit( EXIT_FAILURE );
  }


//preenche a lista de nomes
  for ( aux = 0; aux < tamanho; aux++ ) {
    fgets( buffer, 256, fp );
    removespaco( buffer );
    nome = calloc( strlen( buffer ) + 1, sizeof( char ) );
    strcpy ( nome, buffer );
    listaNomes[ aux ] = nome;
  }
  // fecha o arquivo
  fclose( fp );
  //imprime a lista de nomes
  for ( aux = 0; aux < tamanho; aux++ )
    printf( "%s\n", listaNomes[ aux ] );

//libera a memoria alocada for ( aux = 0; aux < tamanho; aux++ ) { nome = listaNomes[ aux ]; free( nome ); } free( listaNomes );

return EXIT_SUCCESS; } //função para remover o espaço entre os dados. void removespaco( char *str ) { int tamanho; tamanho = strlen( str ); if ( str[ tamanho -1 ] == '\n' ) str[ tamanho - 1 ] = '


#include &ltstdio.h&gt
#include &ltstdlib.h&gt
#include &ltstring.h&gt

void removeNovaLinha( char *str );

int main( int argc, char *argv[] )
{
  FILE *fp;
  int aux, tamanho;
  char buffer[ 256 ];
  char **listaNomes;
  char *nome;

  fp = fopen( "documento.txt", "r" );//digitar o nome do documento que esta na mesma pasta que o programa .c
  //o arquivo deverá conter o "documento.txt" com os dados criados pelo Rafael
  if ( fp == NULL ) {
    printf( "Erro: nao posso abrir o arquivo %s!\n","documento.txt"  );

    exit( EXIT_FAILURE );
  }
  if ( !fscanf( fp, "%d\n", &tamanho ) ) {
    printf( "Erro: O arquivo deve comecar com um num. inteiro\n" );

    exit( EXIT_FAILURE );
  }

  listaNomes = calloc( tamanho, sizeof( char * ) );
  if ( listaNomes == NULL ) {
    printf( "Erro: nao posso alocar memoria!\n" );

    exit( EXIT_FAILURE );
  }
'; }'

  Thank you very much in advance.     
asked by anonymous 01.11.2016 / 14:34

1 answer

2

There are some errors in your code. - > Read integer in file. - > Inappropriate use of hands. - > Incorrect dynamic allocation. char **listaNomes should only be a pointer to pointer if you are doing dynamic array allocation and your dynamic allocation is incorrect. The correct way would be: First you must declare a pointer to pointer type char

char **listaNomes;

After this you must allocate these pointers to pointers so you can create a vector of vectors that behaves like a matrix, so it would be:

listaNomes = (char **) malloc(tamanho * sizeof(char *));

After this, a vector of pointers has been generated for pointers, so now we must allocate other pro char vectors that would be:

for(i = 0 ; i < 10 ; i ++)
    listaNomes[ i ] = (char *) malloc (10 *sizeof(char));

You have made use of calloc instead of malloc, since it starts every integer variable with zero in the case of numerical types and with '\ 0' for char type.

To read a numeral character from the file you need after reading it passes it to the atoi () function that takes a string as a parameter and returns an integer, that is:

char string[ 2 ] = {'3', '4'};
int num = atoi(str);
num agora guarda 34.

Because your program should sort data by name, color, and size, you should read the file string more dynamically so you can separate name, color, and size into different variables. Since in C everything is possible I will say yes it is possible to sort this data within the string but it will be much more complicated then I advise you to break this string into different variables. By following these instructions I passed you will gradually be able to model your program the way you want. Thanks.

    
03.11.2016 / 20:26