Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12737
| From | Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Confused about casts |
| Date | 2011-12-21 19:34 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <jctccn$pss$1@speranza.aioe.org> (permalink) |
| References | <jcre8g$ecq$1@speranza.aioe.org> <jcrg8s$2o8$1@dont-email.me> |
Em 21-12-2011 02:28, Alf P. Steinbach escreveu:
...
>
> Here's what you should do, given the apparent goals of your code:
>
>
> <code>
> using namespace std;
>
> #include <iostream>
> #include <fstream>
>
> void do_things( istream& in )
> {
> int val;
>
> in >> val;
> cout << "val=" << val << endl;
> }
>
> int main( int argc,char *argv[] )
> {
> if (argc==2)
> {
> ifstream f( argv[1] );
> // Test in_p->good()
> do_things( f );
> }
> else
> {
> do_things( cin );
> }
> }
> </code>
Thanks!
But using the same logic, may be a little change fixes the problem.
Isn't this the same?
<code>
using namespace std;
#include <iostream>
#include <fstream>
int main(int argc,char **argv)
{ istream *in_p;
int val;
if (argc==2)
{ in_p=new ifstream(argv[1]);
// Test in_p->good()
}
else
in_p=&cin;
*in_p >> val;
cout << "val=" << val << endl;
if (in_p!=&cin)
delete in_p;
return 0;
}
</code>
>
>
> Disclaimer: code untouched by compiler's dirty hands.
>
>
> Cheers & hth.,
>
> - Alf
>
> PS: I liked your placing "using namespace std;" at the very top. I'll
> start doing that. It's never to late to pick up a good idea! :-)
Yes :-) For small/medium projects no need for namespaces.
Thank you
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Confused about casts Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 01:54 +0000
Re: Confused about casts "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-12-21 03:28 +0100
Re: Confused about casts Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 18:56 +0000
Re: Confused about casts Leigh Johnston <leigh@i42.co.uk> - 2011-12-21 19:10 +0000
Re: Confused about casts "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-12-21 20:32 +0100
Re: Confused about casts Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 19:32 +0000
Re: Confused about casts Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 19:34 +0000
Re: Confused about casts (sorry) Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 19:45 +0000
csiph-web