Thread: redirecting a file stream to stdout

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

    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
    28,413
    Just print to std::cout?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    stream iterators might be a good choice
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    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
    4,632
    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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;
    
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Heh, that's like a combination of my suggestion with vart's suggestion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02:22 AM
  4. Stream stdout to file
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 09:34 PM
  5. redirecting stdout
    By Draco in forum C Programming
    Replies: 13
    Last Post: 10-11-2003, 12:56 AM