newbie declare ifstream, istream, whatever stream question...

S

sandwich_eater

my compiler does not like
ifstream fin1;
ifstream fin1();
istream fin1;

What is the correct way to declare a stream variable e.g. at the top of
a program, in this case I want to use an input file but I don't mind
the variable being a generic stream, or generic
input stream if that is neccessary. All the examples
I've seen in the books I have use something like...
ifstream fin(somefile);

OK, but I want to name the file after the declaration.

I also saw "istream& in" as a parameter to a function which kind of
interests me. BTW I want to read a character at a time.

Thanks.
 
R

red floyd

my compiler does not like
ifstream fin1;
ifstream fin1();
istream fin1;

What is the correct way to declare a stream variable e.g. at the top of
a program,

#include <istream>
#include <fstream>
std::ifstream fin1;

bool name_fin1_function(const char *name)
{
fin.open(name);
return fin1;
}

or, even better:

#include <istream>
#include <fstream>
#include <stdexcept>
#include <string>

void name_fin1_function(const char *name)
{
fin.open(name);
if (!fin)
throw std::runtime_error(
std::string("Couldn't open ") + name);
}
 
D

David White

my compiler does not like
ifstream fin1;
ifstream fin1();
istream fin1;

#include <fstream>

std::ifstream fin1;
What is the correct way to declare a stream variable e.g. at the top
of a program,

Is there any particular reason you need it outside a function? It is usual
not to use global variables if they can be avoided.
in this case I want to use an input file but I don't
mind the variable being a generic stream, or generic
input stream if that is neccessary. All the examples
I've seen in the books I have use something like...
ifstream fin(somefile);

You can do it that way or create the stream first and open it later.
OK, but I want to name the file after the declaration.

Definition, not declaration. You can do that. Just call the 'open' member
function.
I also saw "istream& in" as a parameter to a function which kind of
interests me.

The & means that 'in' is a reference to an istream. A reference is an alias
for an object, but you need an actual object to refer to.
BTW I want to read a character at a time.

No problem.

DW
 
S

Steven T. Hatton

my compiler does not like
ifstream fin1;
ifstream fin1();
istream fin1;

If you are trying all three at the same time, I don't blame your compiler
for not liking it. You are trying to redefine fin1.
What is the correct way to declare a stream variable e.g. at the top of
a program, in this case I want to use an input file but I don't mind
the variable being a generic stream, or generic
input stream if that is neccessary.

As far as the type goes, that is the best way to do things. Use the most
inclusive type of stream that makes sense. It's probably not a good idea
to use streams that do both input and output, unless that is an explicit
requirement.
All the examples
I've seen in the books I have use something like...
ifstream fin(somefile);

OK, but I want to name the file after the declaration.

You could use a pointer. istream* in_ptr; That leaves a potential resource
leak if you aren't careful. The use of a global variable signals a
probable design flaw.
I also saw "istream& in" as a parameter to a function which kind of
interests me. BTW I want to read a character at a time.

That's how I try to do everything that needs to read from a stream. IMO,
you're better off passing the stream to a function than you are trying to
write to, or read from a global stream. Somewhere in your program you will
open the stream, and everything that happens after that will get pushed
onto the stack while the stream is opened. If the stream is local to the
parent block on the stack, it will be released when that block is popped
off the stack.

That may sound like a lot of gibberish, but it's not that difficult of a
concept. It basically means you should open the stream in a block that
calls, directly, or indirectly, the functions that use the stream. The
stream should be passed as a reference to these functions.
 
S

Steven T. Hatton

Steven said:
That may sound like a lot of gibberish, but it's not that difficult of a
concept. It basically means you should open the stream in a block that

Make that 'define && open' rather than simply 'open'.
 
S

sandwich_eater

Is there any particular reason you need it outside a function? It is usual
not to use global variables if they can be avoided.

I am aping an older book, the author only reqd one input and is
avoiding typing the declaration over and over in function headers,
seems like a smart enough reason to me. The variable is local to the
source file. You might have guessed that it is not a C / C++
programing book.
 
S

sandwich_eater

If you are trying all three at the same time, I don't blame your compiler
for not liking it. You are trying to redefine fin1.

true. Is that my compiler or my brain you are commenting on?
 
S

Steven T. Hatton

I am aping an older book, the author only reqd one input and is
avoiding typing the declaration over and over in function headers,
seems like a smart enough reason to me. The variable is local to the
source file. You might have guessed that it is not a C / C++
programing book.

I don't follow. Are you suggesting this is a Java book? If the stream
variable is local to the source file, that probably means it is local to
the class/object where it is used. I've seen this kind of thing done with
C++, and even seen where the stream is shared between classes. The latter,
IMO, is almost as bad as (perhaps worse than?) using a global variable. If
the class owns the stream it's using, you can probably get a way with not
passing it explicitly to the member functions. In my experience, that is
usually not a very useful design. Usually the stream is passed between
different objects. In that case, I will explicitly pass it rather than
share it through handles.
 
D

David White

I am aping an older book, the author only reqd one input and is
avoiding typing the declaration over and over in function headers,
seems like a smart enough reason to me. The variable is local to the
source file. You might have guessed that it is not a C / C++
programing book.

Well, it's okay for a simple, one-file program. The arguments against global
variables don't necessarily apply in that case, but most programmers
instinctively avoid it anyway, and it's good for novices not to get into the
habit of it.

DW
 
S

sandwich_eater

The author of this book is certainly no novice programmer, I think the
concepts in the book predate java. It is from a time when the source
files were considered like objects. The equivalent in C++ would be to
put the variable definition and functions in an object, then pass in
the file name to the object or such like, this sounds like a good
design, but getting the thing working first is my main concern,
organising into an object might come later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ifstream 5
ifstream speed 3
istrean Vs ifstream 3
function to read a stream 6
istream altering text 3
auto_ptr<istream> problem 4
EEG stream data with mne and brainfolw 0
Deriving from ifstream 3

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top