[C - Linux] problème avec scanf

problème avec scanf [C - Linux] - Programmation

Marsh Posté le 04-01-2002 à 20:56:08    

Bonjour,
 
je comprends pas pourquoi quand je fais:
 
scanf("%s",chaine);
printf("%s",chaine);
 
et que la chaîne saisie est "totototo to to to to" le programme m' affiche
 
totototo
 
Comment faire pour éviter çà ??
Merci.

Reply

Marsh Posté le 04-01-2002 à 20:56:08   

Reply

Marsh Posté le 04-01-2002 à 20:59:47    

Essaie gets, par contre je l'utilise jamais donc regarde dans la doc ;)

Reply

Marsh Posté le 04-01-2002 à 22:42:31    

nikewlas a écrit a écrit :

Bonjour,
 
je comprends pas pourquoi quand je fais:
 
scanf("%s",chaine);
printf("%s",chaine);
 
et que la chaîne saisie est "totototo to to to to" le programme m' affiche
 
totototo
 
Comment faire pour éviter çà ??
Merci.  




kelle est la declaration de chaine sil te plait????


---------------
Jim
Reply

Marsh Posté le 05-01-2002 à 00:28:39    

C'est très simple le scanf s'arrete au PREMIER  
caractère séparateur rencontré :
genre '\0',' ' (l'espace), tabulation,...
 
ta variable reçoit la chaîne jusqu'au premier ' '
donc  '12345ABCD ABC AB ABCD\0'
donne '12345ABCD\0'(dans la variable)
 
('\0' == le retour chariot, RETURN quoi...)

Reply

Marsh Posté le 05-01-2002 à 00:45:33    

Mon conseil : oublie pas le \n a la fin du printf ( printf("%s\n",chaine) ) pour passer a la ligne et mieux voir ce que tu as écrit.
 
Et ils ont raison, tous la, le scanf s'arrete au premier espace rencontré, et autres aussi ( \0, \n, etc).


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
Reply

Marsh Posté le 05-01-2002 à 01:05:31    

C'est pour ça qu'il y a une fonction qui prend toute la ligne et remplace \n par \0 et qui doit être gets si mes souvenirs sont bons

Reply

Marsh Posté le 05-01-2002 à 01:18:37    

Gets
Syntax
 
#include <stdio.h>
char *gets(char *s);
 
Description
 
Gets a string from stdin.
gets collects a string of characters terminated by a new line from the standard input stream stdin and puts it into s. The new line is replaced by a null character (\0) in s.
gets allows input strings to contain certain whitespace characters (spaces, tabs). gets returns when it encounters a new line; everything up to the new line is copied into s.
The gets function is not length-terminated. If the input string is sufficiently large, data can be overwritten and corrupted. The fgets function provides better control of input strings.
 
Note: For Win32s or Win32 GUI applications, stdin must be redirected.
 
Return Value
 
On success, gets returns the string argument s.
On end-of-file or error, it returns NULL
 
Borland C++ 5.0 Programmer's Guide


---------------
Des bons sites pour Delphi? http://forum.hardware.fr/forum2.php3?post=16838&cat=10 -- informaticien -- http://www.z0rglub.com/phpwebgallery/ -- Delphi :love:
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed