cout vs. ofstream output

Discussion in 'C++' started by roger.a.banks@gmail.com, Jan 11, 2009.

  1. Guest

    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!
     
    , Jan 11, 2009
    #1
    1. Advertising

  2. Joe Smith Guest

    <> 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::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.
     
    Joe Smith, Jan 12, 2009
    #2
    1. Advertising

  3. ctrucza Guest

    On Jan 12, 1:12 am, 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
     
    ctrucza, Jan 12, 2009
    #3
  4. Guest

    On Jan 11, 9:18 pm, ctrucza <> wrote:
    > On Jan 12, 1:12 am, 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!
     
    , Jan 12, 2009
    #4
  5. Guest

    On 12 Jan., 05:45, "" <>
    wrote:
    > > On Jan 12, 1:12 am, 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 <> 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.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
     
    , Jan 12, 2009
    #5
  6. Jim Langston Guest

    <> wrote in message
    news:...
    > 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
     
    Jim Langston, Jan 13, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    614
    Ivan Johansen
    Jul 14, 2005
  2. Joe Hesse

    Use cout as ofstream object

    Joe Hesse, Sep 26, 2007, in forum: C++
    Replies:
    5
    Views:
    512
    Salt_Peter
    Sep 26, 2007
  3. ksamdev
    Replies:
    3
    Views:
    420
    Stuart Redmann
    Sep 13, 2010
  4. ksamdev
    Replies:
    2
    Views:
    1,210
    ksamdev
    Sep 10, 2010
  5. Jeff Flinn
    Replies:
    0
    Views:
    401
    Jeff Flinn
    Sep 11, 2010
Loading...

Share This Page