1
Hello, my problem is this, in the topic created by Rafael.
About problem reading strings.
Who asked about ordering t-shirts in c and then got the answer from Rafael Bluhn.
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 <stdio.h>
#include <stdlib.h>
#include <string.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.
– José Eduardo Kimura
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.
– carlos cunha