Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fgets and variants

Reply
Thread Tools

fgets and variants

 
 
Diego
Guest
Posts: n/a
 
      04-02-2004
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[i] = '\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
 
Reply With Quote
 
 
 
 
those who know me have no need of my name
Guest
Posts: n/a
 
      04-03-2004
in comp.lang.c i read:

>When I run the following code, if I want to introduce a second pile of
>data, the fgets is ignored.


actually it's your lack of understanding of how scanf works and too little
debugging (skill?) that is causing you to think that the second fgets is
being ignored. scanf examines each byte on the input stream, as soon as it
reaches one that no longer is acceptable for the type of conversion being
performed it puts it back onto the stream and finalizes the conversion.
you are probably pressing enter (i.e., '\n') after each of the values. the
%d's have no use for '\n' so it is left on the input stream, and since %d
skips leading whitespace it's not a problem for scanf, but when all your
%d's are done then whatever stopped the last conversion (probably a '\n')
is left on the stream. the second fgets terminates copying as soon as it
copies a '\n' (or one less than the limit), which is likely the first thing.

> char nbarco[41];


> fgets(nbarco, 41, stdin); /* aade newline al final */
> //fgets(nbarco, strlen(nbarco), stdin); /* quita un carcter al final */


since nbarco is an automatic variable without an initializer and not
otherwise assigned prior to this point you cannot use strlen() safely.
further i'm not sure why you would want to read at most as many characters
as the string already happens to contain. the first is the correct usage
of fgets, though i'd probably use sizeof nbarco instead of the `magic'
value 41 -- it's too easy for them to get out of sync otherwise.

--
a signature
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-03-2004
On Sat, 03 Apr 2004 00:15:22 +0200, Diego <(E-Mail Removed)>
wrote:

>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).


If you are going to use scanf and fgets together, you must make sure
that after calling scanf you remove the '\n' that is left in the
stream. Otherwise, the next time you call fgets it reads that
character and believes it is all done.



<<Remove the del for email>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for info on the XP AntiVirus Malware and variants Bob Bob Computer Support 17 11-17-2008 08:01 PM
HOWTO: const and pointer variants in C and C++ Adem C++ 10 11-12-2008 10:02 PM
HOWTO: const and pointer variants in C and C++ Adem C Programming 10 11-12-2008 10:02 PM
Mixed types and variants bearophileHUGS@lycos.com Python 2 11-24-2005 04:56 AM
15454-OC121IR1310 Variants Will Cisco 0 06-29-2005 01:46 AM



Advertisments