Thread: redefine cout to directing one file, is impossible?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    98

    redefine cout to directing one file, is impossible?

    I want to save my test prog processing result showing to other
    newbie, (haha, I am just a new new newbie)
    I think is it possible to change cout direction from monitor to a file?
    just like we often do in dos:
    Code:
    c:\>dir >>test.txt
    The statement can save output directory list.

    How to do in c++?
    Last edited by toysoldier; 08-19-2004 at 12:28 AM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well in dos and linux its >(file) not >>(file)

    If you want to do it inside the program instead of external you'll have to do something like:
    Code:
    std::ofstream outfile ("test.txt");
    std::ostream out = outfile;
    
    out<<"Hello world";
    Then when you want to go back to the console you just change it to
    Code:
    std::ostream out = std::cout;
    Or you could just use the operating system's redirection and save yourself a lot of trouble

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ Thantos :
    Thanks, it's great.

    I just test it,
    Code:
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    std::ofstream outfile ("c:test.txt");
    std::ostream out = outfile;
    but dev-cpp give me some error message like this:
    variable `std:fstream outfile' has initializer but incomplete type
    storage size of `outfile' isn't known
    How to correct it ?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    Code:
    #include <fstream>

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    • well you need to define a main() function
    • get rid of #include <stdio.h>
    • #include <fstream>


    Ok I was slightly off on previoius. Here is an example:
    to console:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
      std::ofstream outfile ("test.txt");
      std::ostream &out = std::cout;
      out<<"Hello World"<<endl;
    }
    To file:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
      std::ofstream outfile ("test.txt");
      std::ostream &out = outfile;
      out<<"Hello World"<<endl;
    }

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ Thantos & Tybalt :
    THANKS for you quickly helpness.

    @ Thantos :

    I just copy your code , it's ok:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;   //I add this statement, otherwise endl cant work;
    
    int main()
    {
      std::ofstream outfile ("e:test.txt");   //why add it in main()? 
      std::ostream &out = outfile;
      out<<"Hello World"<<endl;
        
      return 0;          //add this.
    }
    If I want to save some functions output which no beside in main(),
    Whether is it normal work on function , when put this two statements in main() ?
    Last edited by toysoldier; 08-19-2004 at 01:16 AM.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    using namespace std; //I add this statement, otherwise endl cant work;
    That's because it's std::endl.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Opps yeah I should have had std::endl

    The reason I put it into main was because you need a main function to create a program. If you want to do it on a per function base:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    void foo (std::ostream &, const string &);
    
    int main()
    {
      std::ofstream outfile ("test.txt");
      std::string str = "This is a test of the emergency broadcast system";
      foo(std::cout, str);
      foo(outfile, str);
    }
    
    void foo (std::ostream &out, const string &str)
    {
      out << str << std::endl;
    }

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    To redirect streams, i.e. cout you can do this:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        // Open file for writing
        ofstream output("output.txt");
    
        // Save existing "cout" buffer
        streambuf* saved_buffer = cout.rdbuf();
    
        // Redirect "cout" to file stream "output"
        cout.rdbuf(output.rdbuf());
    
        // Write a line using "cout", this will be redirected to the output file
        cout << "This line will actually go to the output file and not the console." << endl;
    
        // Restore old "cout" buffer
        cout.rdbuf(saved_buffer);
    
        // Write a line using "cout", this will be sent to screen as normal
        cout << "This line will go to the console as normal." << endl;
    
        return 0;
    }
    After running, using just cout, the file will contain the first line of data while the second line will be displayed to the screen.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM