Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cout vs. ofstream output

Reply
Thread Tools

cout vs. ofstream output

 
 
roger.a.banks@gmail.com
Guest
Posts: n/a
 
      01-11-2009
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!
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      01-12-2009

<> wrote:
> 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: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.

 
Reply With Quote
 
 
 
 
ctrucza
Guest
Posts: n/a
 
      01-12-2009
On Jan 12, 1:12*am, roger.a.ba...@gmail.com wrote:
> 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


 
Reply With Quote
 
roger.a.banks@gmail.com
Guest
Posts: n/a
 
      01-12-2009
On Jan 11, 9:18*pm, ctrucza <csaba.tru...@gmail.com> wrote:
> On Jan 12, 1:12*am, roger.a.ba...@gmail.com wrote:
>
> > 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


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!
 
Reply With Quote
 
DerTopper@web.de
Guest
Posts: n/a
 
      01-12-2009
On 12 Jan., 05:45, "roger.a.ba...@gmail.com" <roger.a.ba...@gmail.com>
wrote:
> > On Jan 12, 1:12*am, roger.a.ba...@gmail.com wrote:
> > 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



> On Jan 11, 9:18 pm, ctrucza <csaba.tru...@gmail.com> wrote:
> 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....7ccfffb939532b
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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-13-2009
<> wrote in message
news:31567d2c-fdd6-43e2-8a83-...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to redirect clog to cout and ofstream at the same time? ksamdev C++ 3 09-13-2010 06:28 AM
Re: How to redirect clog to cout and ofstream at the same time? Jeff Flinn C++ 0 09-11-2010 01:51 AM
Re: How to redirect clog to cout and ofstream at the same time? ksamdev C++ 2 09-10-2010 06:41 PM
Use cout as ofstream object Joe Hesse C++ 5 09-26-2007 02:34 PM
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57