D
Diego
Hi,
Using gcc 2.96
This message was suggested by a thread started by Knak on 21/03/04
The question is:
When I run the following code, if I want to introduce a second pile of
data, the fgets is ignored. I've been even tempted to use gets. Please,
compile and try.
I've tested fgets, and a simulation of fgets suggested in the cited
thread. It's quite frustrating to seeall my efforts working one time and
failing when calling fgets a second time from the samefunction.
Please look at the following piece of code. First is a small
program (just the beginning of an exercise, not veryfing any user input),
and then following a variation picked up from the above
cited thread. (Thanks to Minti, isingh AT acm DOT org).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct buque /* El nombre buque no sirve aqu de nada, solo como recordatorio */
{
char nombre[41];
float desplazamiento; /* Toneladas */
float carga; /* Capacidad de carga, toneladas */
float eslora; /* metros */
unsigned short int tripulantes; /* Nmero de tripulantes */
} buque; /* Asigna el tipo struct a buque mediante el typedef */
void alta (buque * flota) /* Si no declaramos un puntero al struct, no pasa los datos
durante la ejecucin de la funcin*/
{
char nbarco[41];
printf ("\n*************ALTA DE BUQUES****************\n\n");
printf ("\nNombre del buque?:\t");
fgets(nbarco, 41, stdin); /* aade newline al final */
//fgets(nbarco, strlen(nbarco), stdin); /* quita un carcter al final */
strcpy( flota->nombre, nbarco );
printf ("\nDesplazamiento en Tm?:\t");
scanf ("%f", &flota->desplazamiento);
printf ("\nCapacidad de carga en Tm?:\t");
scanf ("%f", &flota->carga);
printf ("\nEslora en mts?:\t");
scanf ("%f", &flota->eslora);
printf ("\nNmero de tripulantes.?:\t");
scanf ("%d", &flota->tripulantes);
}
void grabaFichero (FILE *fp, char* formato, buque flota)
{
formato = "\n\
Nombre del buque: \t%-40s\n\
Desplazamiento: \t%6.2f Tm\n\
Carga: \t\t%6.2f Tm\n\
Eslora: \t\t%4.2f mts\n\
Tripulacin: \t%4d\n\n";
fprintf (fp,
formato,
flota.nombre,
flota.desplazamiento,
flota.carga,
flota.eslora,
flota.tripulantes
);
}
void imprime (char * formato, buque flota)
{
formato = "\n\
Nombre del buque: \t%-40s\n\
Desplazamiento: \t%6.2f Tm\n\
Carga: \t\t%6.2f Tm\n\
Eslora: \t\t%4.2f mts\n\
Tripulacin: \t%4d\n\n";
printf ( formato,
flota.nombre,
flota.desplazamiento,
flota.carga,
flota.eslora,
flota.tripulantes
);
}
int main()
{
buque barco;
char * datos;
char correcto = 'n';
char respuesta = 's';
FILE *fichero;
while (NULL == (fichero = fopen("naviera.txt", "a+"))) /* si naviera.txt existe, lo abre
y si no lo crea*/
{
printf ("\nError al abrir el fichero de buques\n");
return -1;
}
while (tolower(respuesta) != 'n')
{
while (tolower(correcto) != 's')
{
alta (&barco);
imprime (datos, barco);
printf ("\nSon correctos los datos del buque (S o N)?: ");
scanf("%s", &correcto);
}
grabaFichero(fichero, datos, barco);
printf ("\nDar de alta ms buques (S o N)?: ");
scanf("%s", &respuesta);
}
fclose(fichero);
return 0;
}
_______________________
Following the variation tested, getting the same result:
(replaces the 1st paragraph following ***Alta....., and of course I've included
the #define VERDAD 1)
int tamano = 1;
char* nbarco = ( char * ) malloc( tamano );
int i = 0;
int c;
while ( VERDAD ) {
c = getchar();
if ( c == '\n' || c == EOF ) { nbarco = '\0' ; break; }
else {
if ( i == tamano ) {
nbarco = ( char * ) realloc ( nbarco, tamano *= 2 );
if ( ! nbarco ) {
exit ( EXIT_FAILURE );
}
}
nbarco[i++] = c;
}
}
Thanks very much for your help,
Diego
damulco AT telefonica DOT net
Using gcc 2.96
This message was suggested by a thread started by Knak on 21/03/04
The question is:
When I run the following code, if I want to introduce a second pile of
data, the fgets is ignored. I've been even tempted to use gets. Please,
compile and try.
I've tested fgets, and a simulation of fgets suggested in the cited
thread. It's quite frustrating to seeall my efforts working one time and
failing when calling fgets a second time from the samefunction.
Please look at the following piece of code. First is a small
program (just the beginning of an exercise, not veryfing any user input),
and then following a variation picked up from the above
cited thread. (Thanks to Minti, isingh AT acm DOT org).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct buque /* El nombre buque no sirve aqu de nada, solo como recordatorio */
{
char nombre[41];
float desplazamiento; /* Toneladas */
float carga; /* Capacidad de carga, toneladas */
float eslora; /* metros */
unsigned short int tripulantes; /* Nmero de tripulantes */
} buque; /* Asigna el tipo struct a buque mediante el typedef */
void alta (buque * flota) /* Si no declaramos un puntero al struct, no pasa los datos
durante la ejecucin de la funcin*/
{
char nbarco[41];
printf ("\n*************ALTA DE BUQUES****************\n\n");
printf ("\nNombre del buque?:\t");
fgets(nbarco, 41, stdin); /* aade newline al final */
//fgets(nbarco, strlen(nbarco), stdin); /* quita un carcter al final */
strcpy( flota->nombre, nbarco );
printf ("\nDesplazamiento en Tm?:\t");
scanf ("%f", &flota->desplazamiento);
printf ("\nCapacidad de carga en Tm?:\t");
scanf ("%f", &flota->carga);
printf ("\nEslora en mts?:\t");
scanf ("%f", &flota->eslora);
printf ("\nNmero de tripulantes.?:\t");
scanf ("%d", &flota->tripulantes);
}
void grabaFichero (FILE *fp, char* formato, buque flota)
{
formato = "\n\
Nombre del buque: \t%-40s\n\
Desplazamiento: \t%6.2f Tm\n\
Carga: \t\t%6.2f Tm\n\
Eslora: \t\t%4.2f mts\n\
Tripulacin: \t%4d\n\n";
fprintf (fp,
formato,
flota.nombre,
flota.desplazamiento,
flota.carga,
flota.eslora,
flota.tripulantes
);
}
void imprime (char * formato, buque flota)
{
formato = "\n\
Nombre del buque: \t%-40s\n\
Desplazamiento: \t%6.2f Tm\n\
Carga: \t\t%6.2f Tm\n\
Eslora: \t\t%4.2f mts\n\
Tripulacin: \t%4d\n\n";
printf ( formato,
flota.nombre,
flota.desplazamiento,
flota.carga,
flota.eslora,
flota.tripulantes
);
}
int main()
{
buque barco;
char * datos;
char correcto = 'n';
char respuesta = 's';
FILE *fichero;
while (NULL == (fichero = fopen("naviera.txt", "a+"))) /* si naviera.txt existe, lo abre
y si no lo crea*/
{
printf ("\nError al abrir el fichero de buques\n");
return -1;
}
while (tolower(respuesta) != 'n')
{
while (tolower(correcto) != 's')
{
alta (&barco);
imprime (datos, barco);
printf ("\nSon correctos los datos del buque (S o N)?: ");
scanf("%s", &correcto);
}
grabaFichero(fichero, datos, barco);
printf ("\nDar de alta ms buques (S o N)?: ");
scanf("%s", &respuesta);
}
fclose(fichero);
return 0;
}
_______________________
Following the variation tested, getting the same result:
(replaces the 1st paragraph following ***Alta....., and of course I've included
the #define VERDAD 1)
int tamano = 1;
char* nbarco = ( char * ) malloc( tamano );
int i = 0;
int c;
while ( VERDAD ) {
c = getchar();
if ( c == '\n' || c == EOF ) { nbarco = '\0' ; break; }
else {
if ( i == tamano ) {
nbarco = ( char * ) realloc ( nbarco, tamano *= 2 );
if ( ! nbarco ) {
exit ( EXIT_FAILURE );
}
}
nbarco[i++] = c;
}
}
Thanks very much for your help,
Diego
damulco AT telefonica DOT net