 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bernhard Georg Enders Guest
|
Posted: Tue Apr 27, 2004 10:41 am Post subject: Simple Problem with GETS, CIN and COUT |
|
|
Hi!
I'm newbie to C++ and I would like to know which would be the best
(elegant and correct) solution for the following small (string) read
problem with gets. Maybe another function?! I'm using Dev-C++ based on
gcc 3.2 compiler, so, please post only ISO-14882 compliant code.
TIA,
Bernhard.
------------ C++98 code ------------
#include<iostream>
using namespace std;
#include<cstdio>
main(){
int n;
int waste; // needs this to work (read) properly!!
char name[51];
cout << "Enter any integer number...n";
cin >> n;
cout << "Enter your name...n";
cin >> waste; // 'gets' does not read the name without this
line!!
gets(name);
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Apr 27, 2004 4:42 pm Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
In message <27928f1f.0404261915.42463345 (AT) posting (DOT) google.com>, Bernhard
Georg Enders <bgeneto (AT) ig (DOT) com.br> writes
| Quote: | I'm newbie to C++ and I would like to know which would be the best
(elegant and correct) solution for the following small (string) read
problem with gets. Maybe another function?! I'm using Dev-C++ based on
gcc 3.2 compiler, so, please post only ISO-14882 compliant code.
|
A large part of your problem is that you are not using the full power of
C++ and instead are writing low level C-style code which places much
greater demands on understanding how things work.
Let me first comment your code and then provide an alternative which is
both safer and cleaner.
| Quote: | TIA,
Bernhard.
------------ C++98 code ------------
#include<iostream
using namespace std;
#include
Probably better style to reverse the order of the above two lines (I am |
not even sure that your code works portably with the order you have
used.)
| Quote: |
main(){
You must declare the return type of all functions including main(). This |
is now true for C as well as having been true for C++ for almost a
decade. So the above line should be:
int main(){
| Quote: | int n;
int waste; // needs this to work (read) properly!!
We will see latter that the above is not needed (and actually does |
nothing useful)
| Quote: | char name[51];
OK let me stick with this C-style for now.
cout << "Enter any integer number...n";
cin >> n;
At this stage either the user has entered a number correctly or cin has |
just gone into a fail state. However even if it worked it still leaves
at least a 'n' in the input buffer (unless you are using a Unix OS and
terminated input with ^D) which will have to be dealt with.
| Quote: | cout << "Enter your name...n";
cin >> waste; // 'gets' does not read the name without this
line!!
|
This is not the way to clear out the garbage in the input buffer (indeed
I am at a loss to know why it even worked on your system -- actually
thinking about it, I think what you did was to cause cin to eat all the
whitespace looking for a number and then place itself in a fail state
when it read a non-whitespace character that was not a numeric char
(digit, + or -) The correct way to achieve your objective (assuming that
^D has not been used to terminate input on a Unix OS) is:
cin.ignore(numeric_limits<int>::max(), 'n');
You will need:
#include <limits>
for the above line to work.
| Quote: | gets(name);
And this switches to C-style input, thus circumventing the fact that cin |
is now in an error state and will not extract any more data from the
input buffer till the problem is corrected. Unfortunately you are now
using a function that every single expert I know says should NEVER be
used because it is lethally dangerous in allowing buffer overruns (a
well known type of exploit by hackers as well as potentially damaging to
your system -- I once reformatted a graphics card when I accidentally
overran the end of a buffer)
If you must use C-style input (not really necessary in C++) use fgets()
and remember to handle the end of line marker. So the above line
becomes:
fgets(name, sizeof(name), stdin);
getc(); // remove end of line character
Now consider the following version
#include<iostream>
#include <string>
#include <cstdlib> // for exit macros
#include <limits>
using namespace std;
int main(){
int n;
string name;
cout << "Enter any integer number...n";
cin >> n;
if(!cin){
cout << "ERROR in input" << endl;
return EXIT_FAILURE;
}
cin.ignore(numeric_limits
cout << "Enter your name...n";
getline(cin, name);
cout << "You typed in: " << n << " and " << name << 'n';
return EXIT_SUCCESS;
}
Yes, it is a little longer than yours but it is safe. It validates the
input and acts on failure.
If the book you are studying does not introduce std::string early on but
uses raw arrays of char and advocates using gets() then please seriously
consider getting a different book; the author is being unhelpful and
encouraging you to do dangerous things (that you will have to unlearn if
you wish to do serious programming in either C or C++).
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Tue Apr 27, 2004 6:17 pm Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
Bernhard Georg Enders wrote:
| Quote: | I'm newbie to C++ and I would like to know which would be the best
(elegant and correct) solution for the following small (string) read
problem with gets. Maybe another function?!
I'm using Dev-C++ based on
gcc 3.2 compiler, so, please post only ISO-14882 compliant code.
|
How about you start doing so first, because
| Quote: | main(){
without a return-value is surely not. |
| Quote: | int n;
cout << "Enter any integer number...n";
cin >> n;
|
User enters "123n", cin parses until it reaches the 'n', decides that this
is not part of the number it is reading and puts it back into the
input-buffer.
| Quote: | cout << "Enter your name...n";
int waste; // needs this to work (read) properly!!
cin >> waste; // 'gets' does not read the name without this line!!
char name[51];
gets(name);
|
Well, firstly, gets() is evil because it potentially causes buffer
overflows. STFW/RTFM for more info. Secondly, if the name you enter is
"123fourn", you end up with waste=123 and name = "four", so it doesn't
work.
What you want is to forget about those old C-style IO functions(which will
not work if you had changed cin's streambuffer) and take a look at
std::string, std::getline() (the function, not the method of std::istream!)
and std::istream::ignore(). Then, you also don't need the coupling with C
stdio streams and can start your program with
std::ios_base::sync_with_stdio(false); to improve performance a bit.
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Apr 28, 2004 9:28 am Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
Bernhard Georg Enders wrote:
| Quote: | Hi!
I'm newbie to C++ and I would like to know which would be the best
(elegant and correct) solution for the following small (string) read
problem with gets. Maybe another function?! I'm using Dev-C++ based on
gcc 3.2 compiler, so, please post only ISO-14882 compliant code.
TIA,
Bernhard.
------------ C++98 code ------------
#include<iostream
using namespace std;
#include
main(){
|
There is no "implicit int" in C++. You must declare the return type
of each function.
| Quote: | int n;
int waste; // needs this to work (read) properly!!
char name[51];
cout << "Enter any integer number...n";
cin >> n;
cout << "Enter your name...n";
cin >> waste; // 'gets' does not read the name without this line!!
|
You presumably want to ignore the rest of the first line of input.
The correct way to do that is:
cin.ignore(std::numeric_limits<int>::max(), 'n');
Never use gets() - it cannot be used safely. Instead, you can declare
name as a std::string and use std::getline(cin, name).
You should also test for input errors along the way.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Apr 28, 2004 7:22 pm Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: | In message <27928f1f.0404261915.42463345 (AT) posting (DOT) google.com>, Bernhard
Georg Enders <bgeneto (AT) ig (DOT) com.br> writes
|
[Francis has mentionned all of the important points, but there is
one point in his answer that I am not sure of (and another where he
has apparently forgotten his C ...]
| Quote: | cout << "Enter your name...n";
cin >> waste; // 'gets' does not read the name without this
line!!
This is not the way to clear out the garbage in the input buffer
(indeed I am at a loss to know why it even worked on your system --
actually thinking about it, I think what you did was to cause cin to
eat all the whitespace looking for a number and then place itself in a
fail state when it read a non-whitespace character that was not a
numeric char (digit, + or -)
|
I'm quite sure that that is what has happened. In doing so, of course,
it HAS extracted the 'n', which was what he probably wanted.
One useful thing to learn to do is to test a program. Including with
strange or illegal input. If my name happened to be "123", I'll bet
that he would not get the results he is looking for:-).
| Quote: | The correct way to achieve your objective (assuming that ^D has not
been used to terminate input on a Unix OS) is:
cin.ignore(numeric_limits<int>::max(), 'n');
You will need:
#include <limits
for the above line to work.
|
I hate to say it, but:
cin.ignore( INT_MAX, 'n' ) ;
is a lot easier to type:-). Of course, he'll need to include
or <limits.h> for it to work.
| Quote: | gets(name);
And this switches to C-style input, thus circumventing the fact that
cin is now in an error state and will not extract any more data from
the input buffer till the problem is corrected. Unfortunately you are
now using a function that every single expert I know says should NEVER
be used because it is lethally dangerous in allowing buffer overruns
(a well known type of exploit by hackers as well as potentially
damaging to your system -- I once reformatted a graphics card when I
accidentally overran the end of a buffer)
|
Put more simply: a correct program doesn't use gets, and no program
which uses gets is correct.
Note that scanf can be similarly dangerous, if you use a "%s" without a
length, as can sprintf. Or the >> operator in C++, if the target is a
char[].
| Quote: | If you must use C-style input (not really necessary in C++) use
fgets() and remember to handle the end of line marker. So the above
line becomes:
fgets(name, sizeof(name), stdin);
|
I'm not sure, but is it really guaranteed that you can mix stdin and cin
with impunity. Generally speaking (and the C standard requires it), if
standard in is connected to an interactive device, there will be no
buffering, or at the most, line buffering, so reading one line with cin,
and the next with fgets, will work. But is it also guaranteed to work
if standard in is redirected to read from a file?
All I could find with a quick glance in the standard is "The object cin
controls input from a stream buffer associated with the object stdin,
declared in <cstdio>>" Which sounds like the intent at least was that it
be guaranteed, but the standard doesn't seem to say anything elsewhere
with regards to associating an std::istream with a FILE*.
If the std::cin and stdin do not share the same buffer, and input is
buffered, then there is a good chance that the data fgets is supposed to
read has already been extracted into the buffer associated with
std::cin. (This is the case with at least one of the compilers I use,
Sun CC. I don't know whether it is a bug in Sun CC, however, or whether
the other two compilers, g++ and VC++ have simply implemented an
additional feature to make life a little more convenient.)
| Quote: | getc(); // remove end of line character
|
This is already been done in fgets. In fact, there is another important
difference between gets and fgets which you fail to mention: although
both extract the terminating 'n', fgets inserts it into the string,
whereas gets doesn't. (Thus is vaguely related to the fact that fgets
can stop before the end of line; without the 'n', there would be no way
to know whether this had occurred or not. Although frankly, I prefer
the behavior of get/getline in istream -- if you can't read enough to
find the terminator, it is an error.)
| Quote: | }
Now consider the following version
#include<iostream
#include
#include
#include <limits
using namespace std;
int main(){
int n;
string name;
cout << "Enter any integer number...n";
cin >> n;
if(!cin){
cout << "ERROR in input" << endl;
return EXIT_FAILURE;
}
cin.ignore(numeric_limits
cout << "Enter your name...n";
getline(cin, name);
cout << "You typed in: " << n << " and " << name << 'n';
return EXIT_SUCCESS;
}
Yes, it is a little longer than yours but it is safe. It validates the
input and acts on failure.
|
Sort of. For a very loose definition of "validates" -- if I enter
something like 1O instead of 10 as the number, it won't spot the error;
ditto if I mistakenly enter my name on the same line as the number
(because I know what question is coming up).
But I think you are aware of this. Really validating input is complex,
and perhaps a bit too much to throw at someone who still hasn't mastered
the simpler things. (From my own experience, code handling input is, on
an average, about 10 times longer than code handling similar output.
For output, you know what you have, and what you want, whereas for input
you have to be ready for anything.)
| Quote: | If the book you are studying does not introduce std::string early on
but uses raw arrays of char and advocates using gets() then please
seriously consider getting a different book; the author is being
unhelpful and encouraging you to do dangerous things (that you will
have to unlearn if you wish to do serious programming in either C or
C++).
|
IMHO, there are two separate points. If the book uses char[] instead of
std::stirng, it might be that it is simply old. As he is apparently
just starting C++, a newer book would be a good thing, but that doesn't
mean that his present book is bad. If the book recommends gets,
however, it is BAD. Period. The problems with gets were already known
and recognized when I was learning C (more than twenty years ago).
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bernhard Georg Enders Guest
|
Posted: Wed Apr 28, 2004 7:28 pm Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
On 27 Apr 2004 12:42:50 -0400, Francis Glassborow
<francis (AT) robinton (DOT) demon.co.uk> wrote:
| Quote: | #include<iostream
using namespace std;
#include
Probably better style to reverse the order of the above two lines (I am
not even sure that your code works portably with the order you have
used.)
|
Agreed.
| Quote: |
main(){
You must declare the return type of all functions including main(). This
is now true for C as well as having been true for C++ for almost a
decade. So the above line should be:
|
Sorry, my fault!
| Quote: | int main(){
int n;
int waste; // needs this to work (read) properly!!
We will see latter that the above is not needed (and actually does
nothing useful)
char name[51];
OK let me stick with this C-style for now.
cout << "Enter any integer number...n";
cin >> n;
At this stage either the user has entered a number correctly or cin has
just gone into a fail state. However even if it worked it still leaves
at least a 'n' in the input buffer (unless you are using a Unix OS and
terminated input with ^D) which will have to be dealt with.
cout << "Enter your name...n";
cin >> waste; // 'gets' does not read the name without this
line!!
This is not the way to clear out the garbage in the input buffer (indeed
I am at a loss to know why it even worked on your system -- actually
thinking about it, I think what you did was to cause cin to eat all the
whitespace looking for a number and then place itself in a fail state
when it read a non-whitespace character that was not a numeric char
(digit, + or -) The correct way to achieve your objective (assuming that
^D has not been used to terminate input on a Unix OS) is:
cin.ignore(numeric_limits<int>::max(), 'n');
You will need:
#include <limits
for the above line to work.
gets(name);
And this switches to C-style input, thus circumventing the fact that cin
is now in an error state and will not extract any more data from the
input buffer till the problem is corrected. Unfortunately you are now
using a function that every single expert I know says should NEVER be
used because it is lethally dangerous in allowing buffer overruns (a
well known type of exploit by hackers as well as potentially damaging to
your system -- I once reformatted a graphics card when I accidentally
overran the end of a buffer)
If you must use C-style input (not really necessary in C++) use fgets()
and remember to handle the end of line marker. So the above line
becomes:
fgets(name, sizeof(name), stdin);
getc(); // remove end of line character
}
Now consider the following version
#include
#include
#include
#include <limits
using namespace std;
int main(){
int n;
string name;
cout << "Enter any integer number...n";
cin >> n;
if(!cin){
cout << "ERROR in input" << endl;
return EXIT_FAILURE;
}
cin.ignore(numeric_limits
cout << "Enter your name...n";
getline(cin, name);
cout << "You typed in: " << n << " and " << name << 'n';
return EXIT_SUCCESS;
}
Yes, it is a little longer than yours but it is safe. It validates the
input and acts on failure.
If the book you are studying does not introduce std::string early on but
uses raw arrays of char and advocates using gets() then please seriously
consider getting a different book; the author is being unhelpful and
encouraging you to do dangerous things (that you will have to unlearn if
you wish to do serious programming in either C or C++).
|
In fact I'm using a worldwide broadcast book and it does not introduce
std::string, it does not advocates using gets() but introduces this
function first for reading strings. I didn't find any single line about
cin.ignore. I will definitively follow your recommendation above.
Thanks a lot for your clearer reply,
Bernhard.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Thu Apr 29, 2004 11:45 am Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] writes:
[snip]
| Quote: | I'm not sure, but is it really guaranteed that you can mix stdin and cin
with impunity.
[snip] |
I thought this was the intent of std::ios_base::sync_with_stdio() (See
27.4.2.4)
By default, stdin and cin are synchronized. Call
std::ios_base::sync_with_stdio(false) before the first I/O to
disable synchronization (which results in big performance gains
for at least one implementation (gcc on non-linux).) (Calling
sync_with_stdio after the first I/O is implementation-defined.)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (nee Spang Guest
|
Posted: Thu Apr 29, 2004 9:21 pm Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
Good morning, James Kanze!
[email]kanze (AT) gabi-soft (DOT) fr[/email] schrieb:
| Quote: | I hate to say it, but:
cin.ignore( INT_MAX, 'n' ) ;
is a lot easier to type:-). Of course, he'll need to include <climits
or
|
Nipicking: The correct way seems to be
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
which besides Francis's requirements demands an additional
#include <ios>
;-)) (I bet someone will find something to nitpick here, also...)
Greetings from Bremen,
Daniel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Apr 30, 2004 11:11 am Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
In message <4090AEC3.1040805 (AT) bdal (DOT) de>, "Daniel Krügler (nee
Spangenberg)" <dsp (AT) bdal (DOT) de> writes
| Quote: | Nipicking: The correct way seems to be
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
which besides Francis's requirements demands an additional
|
However the statement I gave comes from 'The C++ Standard Library' by
Nico Josuttis. I think I am happy with his code as being neither too
much nor too little. (Reminiscent of the story of the three bears)
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Sat May 01, 2004 2:51 am Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
"Daniel Krügler (nee Spangenberg)" <dsp (AT) bdal (DOT) de> wrote
| Quote: | kanze (AT) gabi-soft (DOT) fr schrieb:
I hate to say it, but:
cin.ignore( INT_MAX, 'n' ) ;
is a lot easier to type:-). Of course, he'll need to include
climits> or <limits.h> for it to work.
Nipicking: The correct way seems to be
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
which besides Francis's requirements demands an additional
#include <ios
;-)) (I bet someone will find something to nitpick here, also...)
|
Yes. Wrong nit. You seem to be making a serious error -- you expect
some coherence in the standard:-).
For some strange reason, the first parameter of istream::ignore, in
§27.6.1.3/24, is of type int, and the description says that extraction
stops "if n != numeric_limits
Of course, in the synthesis of the class, in §27.6.1.1, the first
parameter has type streamsize, so your not the only one who is confused.
There is a TC (#172) on this. (It is an obvious error.) Apparently,
the committee decided that you are right, and it (and Francis and I) was
wrong -- the type should be streamsize.
In practice, of course, any large value will generally do the trick.
(Unless, of course, the value is larger than
std::numeric_limits<streamsize>::max().)
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Sat May 01, 2004 2:52 am Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
llewelly <llewelly.at (AT) xmission (DOT) dot.com> wrote
| Quote: | kanze (AT) gabi-soft (DOT) fr writes:
[snip]
I'm not sure, but is it really guaranteed that you can mix stdin
and cin with impunity.
[snip]
I thought this was the intent of std::ios_base::sync_with_stdio() (See
27.4.2.4)
By default, stdin and cin are synchronized. Call
std::ios_base::sync_with_stdio(false) before the first I/O to
disable synchronization (which results in big performance gains
for at least one implementation (gcc on non-linux).) (Calling
sync_with_stdio after the first I/O is implementation-defined.)
|
I knew that there was something. I think that this is something that
was added by the committee, although it might also be something that was
in the USL classical iostreams, but which some other implementations
forgot. I do remember having problems mixing the two, with some
compiler a long time ago.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon May 03, 2004 9:53 am Post subject: Re: Simple Problem with GETS, CIN and COUT |
|
|
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: | In message <4090AEC3.1040805 (AT) bdal (DOT) de>, "Daniel Krügler (nee
Spangenberg)" <dsp (AT) bdal (DOT) de> writes
Nipicking: The correct way seems to be
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
which besides Francis's requirements demands an additional
However the statement I gave comes from 'The C++ Standard Library' by
Nico Josuttis. I think I am happy with his code as being neither too
much nor too little. (Reminiscent of the story of the three bears)
|
The problem is that it is wrong. Now, at least -- it was correct
according to the 1998 version of the standard.
In the unlikely event that streamsize is smaller than an int, you get
implementation defined behavior, which could even be a signal (and a
core dump, if you're not catching that signal) -- you're unlikely to get
a correctly functionning program, however. (But as I say, the case is
extremely unlikely.)
If streamsize is larger than int (generally the case today, I think),
then you could potentially stop ignoring after INT_MAX characters, even
though you have not yet reached either end of file or 'n'. In
practice, I doubt that this would ever be a problem -- I'm a stickler
for quality, but I'm willing to admit that my programs might have
problems with lines longer than 2 billion characters in a text oriented
file (provided, of course, they signal the problem, and don't crash
because of it). But if the point is just that you are willing to accept
a lot of characters, without insisting on accepting an infinite number,
then std::numeric_limits< int >::max() is a lot of characters to type,
given that it has for all intents and purposes the same effect as
1000000 (or even 10000, which is portable to ALL C++ implementations).
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| 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
|
|