 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eric Bart Guest
|
Posted: Fri May 21, 2004 12:49 pm Post subject: Comment lire simplement une ligne sur le flot d'entrée cin |
|
|
Bonjour,
j'ai trouvé ça dans le fichier "/usr/include/c++/3.3.1/iostream" :
get(__streambuf_type& __sb)
{ return this->get(__sb, this->widen('n')); }
Comment l'utiliser avec une simple string ?
Merci
|
|
| Back to top |
|
 |
Jean-Marc Bourguet Guest
|
|
| Back to top |
|
 |
giova Guest
|
Posted: Fri May 21, 2004 1:06 pm Post subject: Re: Comment lire simplement une ligne sur le flot d'entrée |
|
|
Jean-Marc Bourguet wrote:
| Quote: | "Eric Bart" <eb-adm (AT) eric-bart (DOT) pasdepubmerci.net> writes:
#include <iostream
#include
std::string s;
std::getline(std::cin, s);
if (!std::cin) {
// probleme de lecture a traiter
} else {
...
}
A+
ou bien plus simple a coder mais pose probleme si le texte saisie |
comprend des séparateurs tel que caratere espace, tabulation, etc...
#include
#include <string>
using namespace std; //evite d'avoir a taper std:: a tout bout de champ
string s;
cout<<"tape un truc puis touche entrée : ";
cin>>s;
|
|
| Back to top |
|
 |
Eric Bart Guest
|
Posted: Fri May 21, 2004 2:18 pm Post subject: Re: Comment lire simplement une ligne sur le flot d'entrée |
|
|
Ouais super. Merci.
|
|
| Back to top |
|
 |
Michel Michaud Guest
|
Posted: Fri May 21, 2004 2:20 pm Post subject: Re: Comment lire simplement une ligne sur le flot d'entrée |
|
|
Dans news:pxbn0426iwj.fsf (AT) news (DOT) bourguet.org, Jean-Marc
Bourguet <jm (AT) bourguet (DOT) org> a écrit :
| Quote: | std::getline(std::cin, s);
if (!std::cin) {
// probleme de lecture a traiter
} else {
...
}
|
Il me semble que l'idiome consacré (qui, je le sais,
revient strictement au même) serait plutôt
if ( ! getline(cin, s))
{
// Problème de lecture à traiter
...
}
else
{
// s a été lu...
...
}
ou encore
if (getline(cin, s))
{
// s a été lu...
...
}
else
{
// Problème de lecture à traiter
...
}
Il y a là une vision d'un getline indiquant sa réussite,
alors que if (!cin) me paraît plus artificiel et plus C
que C++. Dans les deux cas, c'est en fait cin.fail() qui
est testé indirectement. J'accepterais d'ailleurs plus
volontier if (cin.fail()) que if (!cin)...
(J'espère ne pas ouvrir ici une guerre de religion !)
--
Michel Michaud [email]mm (AT) gdzid (DOT) com[/email]
http://www.gdzid.com
FAQ de fr.comp.lang.c++ :
http://www.cmla.ens-cachan.fr/~dosreis/C++/FAQ/
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|