Like Tree2Likes
  • 1 Post By jimblumberg
  • 1 Post By laserlight

redirecting a file stream to stdout

This is a discussion on redirecting a file stream to stdout within the C++ Programming forums, part of the General Programming Boards category; Hi, Let us assume I have a program that takes the name of the output file from the command line. ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    176

    redirecting a file stream to stdout

    Hi,


    Let us assume I have a program that takes the name of the output file from the command line. Now let us assume that a decided not to give any output file location but wanted my program to to print my strings/ints ... directly to stdout. how would one do this using file streams? Using file pointers and c that is pretty straight forward but how would i achieve this in c++.

    thnx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,591
    Just print to std::cout?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,349
    stream iterators might be a good choice
    Code:
    namespace life
    {
        const bool change = true;
    }

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    176
    yes that is true but then i need at print line make that distinction :

    Code:
    
    if(/*file*/)
      fs.open();
    
    // and then 
    
    
    if(/*file*/)
      fs << "    foo\n";
    else
      cout<< "  foo\n";
    
    // then again somewhere in the code 
    
    
    if(/*file*/)
      fs << "    bar\n";
    else
      cout<< "  bar\n";
    but what i used to do is to define my fp in the beginning and then just print, without checking the file existence every time, like this

    Code:
    if(/*file exists*/)
         fd = open();
    else
        fd = 0;
    Is this more clear?? Is it a good practice ???
    Last edited by baxy; 09-20-2013 at 12:11 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    Why are you going through all that trouble? If you just write your program to write to cout, then redirect the output to a file with file redirection it would be much simpler.

    Another option would be to use rdbuf() to reassign cout's buffer to your file.

    Jim
    Elkvis likes this.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,591
    Quote Originally Posted by baxy
    yes that is true but then i need at print line make that distinction
    No, you don't. Write functions that operate on std::ostream. Then, you can pass std::cout, or some other output stream, by reference to the function that calls the others.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Or just create a reference to the stream you are going to use and send all output to this reference

    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
        std::ofstream file("test.txt");
    
        std::ostream& used_stream = file;
    
        used_stream << "Test\n";
    
        std::ostream& used_stream2 = std::cout;
    
        used_stream2 << "Test\n";
    
        return 0;
    
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    176
    Quote Originally Posted by laserlight View Post
    No, you don't. Write functions that operate on std::ostream. Then, you can pass std::cout, or some other output stream, by reference to the function that calls the others.

    yes, but then again when call the function you need to make this distinction ... right or am i misunderstanding something ?

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by baxy View Post
    yes, but then again when call the function you need to make this distinction
    No you don't

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    static void writeLine(std::ostream& ostream, std::string var);
    int main()
    {
        writeLine(std::cout, "Test\n");
    
        std::ofstream file("test.txt");
    
        writeLine(file, "Test\n");
    }
    
    static void writeLine(std::ostream& ostream, std::string var)
    {
        ostream << var;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,591
    Quote Originally Posted by baxy
    yes, but then again when call the function you need to make this distinction ... right or am i misunderstanding something ?
    Yes, but that is only at one place.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Maybe you could do something like this:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    void write(std::ostream& os, const std::string& s)
    {
        os << s << '\n';
    }
    
    int main(int argc, char **argv)
    {
        if (argc != 2) {
            std::cerr << "Usage: " << argv[0] << " -c | filename\n";
            return 1;
        }
    
        std::ofstream ofs;
        std::ostream& os = std::string(argv[1]) == "-c"
                         ? std::cout
                         : (ofs.open(argv[1]), ofs);
    
        write(os, "hello");
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,591
    Heh, that's like a combination of my suggestion with vart's suggestion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    Heh, that's like a combination of my suggestion with vart's suggestion.
    Exactly! That's what I did all right.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirecting stdout
    By DeanWinchester in forum C Programming
    Replies: 7
    Last Post: 05-25-2013, 09:40 AM
  2. redirecting stdout
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2006, 09:20 PM
  3. Redirecting stdout from XEV
    By 3saul in forum Linux Programming
    Replies: 3
    Last Post: 02-28-2006, 01:22 AM
  4. Stream stdout to file
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 08:34 PM
  5. redirecting stdout
    By Draco in forum C Programming
    Replies: 13
    Last Post: 10-11-2003, 12:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21