cout vs. ofstream output

R

roger.a.banks

I've written a little command line utility. So far it just reads from
stdin using cin and outputs to stdout using cout. I would like to
generalize it some by being able to specify input and output files on
the command line while retaining the ability to use stdin and stdout
if no files are specified on the command line. My thinking is that I
need to replace all instances of cin and cout with some generic names
like 'infile' and 'outfile' and then set them to point to either (cin
| cout) or the file specified on the command line after parsing the
command line, but I can't seem to find the correct way to code this.
Is this the correct approach and, if so, could someone post a hint and/
or sample code? Thanks!
 
J

Joe Smith

I've written a little command line utility. So far it just reads from
stdin using cin and outputs to stdout using cout. I would like to
generalize it some by being able to specify input and output files on
the command line while retaining the ability to use stdin and stdout
if no files are specified on the command line. My thinking is that I
need to replace all instances of cin and cout with some generic names
like 'infile' and 'outfile' and then set them to point to either (cin
| cout) or the file specified on the command line after parsing the
command line, but I can't seem to find the correct way to code this.
Is this the correct approach and, if so, could someone post a hint and/
or sample code? Thanks!


Sure. Here is some example code showing how such a infile could be coded:

int main(int argc, char** argv)
{
bool usecin;
std::string infilename;
// fill in those variables

std::ifstream ifs;
if (!usecin) ifs.open(infilename.c_str());
std::istream& infile = (usecin)?(std::cin):ifs;

//later:
int i;
infile >> i;
return i;
}

I'm sure you can figure out how to add the outfile variable to the above.
Notice the fact that infile is a reference. That is the easiest way to just
make this work.

Unfortunately, this particular solution form could be problematic if
additional user-defined functions are used, as that would nesesiatate
additional parameters to the functions. (namely a "std::istream& infile"
parameter and a "std::eek:stream& outfile" parameter. )

I've seen solution that involved replacing the streambuf of std::cin and
std::cout, but those don't seem very clean to me.
 
C

ctrucza

I've written a little command line utility. So far it just reads from
stdin using cin and outputs to stdout using cout. I would like to
generalize it some by being able to specify input and output files on
the command line while retaining the ability to use stdin and stdout
if no files are specified on the command line.

On most operating systems you could do some form of redirection,
without changing your source:

On unix and windows(dos) variants:

myprog <input.txt >output.txt
 
R

roger.a.banks

On most operating systems you could do some form of redirection,
without changing your source:

On unix and windows(dos) variants:

myprog <input.txt >output.txt

This is what I am doing now. I just wanted to try and generalize it a
bit by being able to take filenames from the command line.

Thanks to everyone that responded!
 
D

DerTopper

This is what I am doing now. I just wanted to try and generalize it a
bit by being able to take filenames from the command line.

Have a look at http://groups.google.com/group/comp.lang.c++/msg/f77ccfffb939532b
This leaves you the opportunity to have a look at the command line
parameters so that your application decides whether to redirect or
not. Although I have to admit that the solution from Sam seems a bit
cleaner since you don't need to clean up the cin read buffer.

Regards,
Stuart
 
J

Jim Langston

I've written a little command line utility. So far it just reads from
stdin using cin and outputs to stdout using cout. I would like to
generalize it some by being able to specify input and output files on
the command line while retaining the ability to use stdin and stdout
if no files are specified on the command line. My thinking is that I
need to replace all instances of cin and cout with some generic names
like 'infile' and 'outfile' and then set them to point to either (cin
| cout) or the file specified on the command line after parsing the
command line, but I can't seem to find the correct way to code this.
Is this the correct approach and, if so, could someone post a hint and/
or sample code? Thanks!

As stated you might just want to leave your program alone and use the OS to
redirect cin and cout for you. Example:

MyProgram
would run it accepting input from cin and outputing to cout.
MyProgram < MyFile.ext > MyFile2.ext
woudl run it accepting input from MyFile.ext and outputting to MyFile2.ext

This syntax works in Lunix, Unix, Dos and Windows. Check your os
documentation for other OSes
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top