problem with file reading/data sorting in c

Asked

Viewed 152 times

1

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

/users/24666/rafael

About problem reading strings.

Problem reading strings

Who asked about ordering t-shirts in c and then got the answer from Rafael Bluhn.

/users/20943/rafael-bluhm

However I want to use with files reading a ".txt file" that will contain the data needed to pass the values to the structure that is in Rafael’s code so ordering name, color, size and then save the changed data in another ".txt file, then print the sorted data on the screen.

I have the part of the code to open the file but I can’t gather the information (which starts with an integer number after strings) to pass to the Rafael structure.

code


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

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 ] );

//releases the allocated memory for ( aux = 0; aux < size; aux++ ) { name = listNomes[ aux ]; free( name ); } free( listing );

Return EXIT_SUCCESS; } //function to remove the space between the data. void removespaco( char *str ) { int size; size = strlen( str ); if ( str[ size -1 ] == ' n' ) str[ size - 1 ] = ' 0'; }` Thanks in advance.

  • Try to get ' n' out of your fscanf first, see if there’s any progress.

  • 1

    Hello, in my case it will make no difference if you take the ' n' of fscanf because only a blank line will appear in the execution of the algorithm and also I want to put the information in the structure of @rafael-Bluhm to order the t-shirts.Thanks for the return.

1 answer

2


There are some errors in your code. -> Read entire file. -> Inappropriate use of pointers. -> Incorrect dynamic allocation. char **listaNomes should only be a pointer to pointer if it is to make dynamic matrix allocation and its dynamic allocation is incorrect. The correct way would be: A char pointer declaration shall be made first

char **listaNomes;

After this, these pointers should be allocated to pointers in order to create a vector vector that behaves like a matrix, so it would be:

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

After this was generated a pointer vector for pointers, so now must be made the allocation of other vectors pro char type that would be:

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

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

To read a numeral character of the file you need after reading pass it to the function atoi() which takes as parameter a string and returns an integer, ie:

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

Since your program should sort the data by name, color and size then 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, so I advise you to break this string into different variables. Following these instructions I gave you you will gradually be able to model your program the way you want it. Hug.

  • Hi J.Carvalho, thanks for the answer, I tested here in the code but gave error of Segmentation fault, and I am not able to join the part of the file with the data ordering because I need to insert the data in the structure and because the number 10 in for?.

  • Hello Carlos sorry for the delay, because well if you have not solved the problem still answer me here I will help you. With respect to the 10 limit is because I pictured it as a 10x10 matrix. Imagine the size variable in the first malloc holding the value 10.

Browser other questions tagged

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