Ciao a tutti,
ho scritto il seguente programma
codice:
void reverse( FILE *fp, char *fileName )
{
char *string = (char*) malloc( 100 * sizeof(char) );
int counter = 0;
while( fgets( string, 100, fp ) != NULL )
++counter;
char **array = (char**) malloc( counter * sizeof(char*) );
fclose( fp );
fp = fopen( fileName, "r" );
int i = 0;
int aux = counter;
while( fgets( string, 100, fp ) )
{
array[--counter] = string;
printf( "%s\n", array[counter] );
}
for( i = 0; i < aux; i++ )
printf( "%s\n", array[i] );
}
che dovrebbe memorizzare le stringhe in un array e poi stamparle in senso inverso.
Le memorizzo direttamente in senso invertito e poi stampo l'array costruito, come potete vedere nei due cicli ho messo 2 printf e, la prima stampa correttamente l'elemento inserito array[counter] mentre il secondo ciclo mi stampa sempre l'ultima stringa letta, come e possibile ?!?!?!
Spero che qualcuno sappia aiutarmi, provatelo pure il main e' cortissimo
codice:
int main( int argc, char** argv )
{
FILE *fp;
if( (fp = fopen( *(argv+1), "r")) == NULL)
{
printf( "Non sono riuscito ad aprire il file\n" );
return -1;
}
reverse( fp, *(argv+1) );
return 1;
}