Results 1 to 6 of 6

Thread: How to show output on both screen and file in c++?

  1. #1
    Join Date
    Aug 2009
    Posts
    59

    How to show output on both screen and file in c++?

    Hello to all,
    I am recently started learning c++ language. I know that using cout method you can show output on the console screen and we use streambuf to save output in file. Sometimes what happened that we want to see output and also we want to save it into file. Can anyone tell me How to show output on both screen and file in c++? Please help me.]
    Thanks in advanced.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to show output on both screen and file in c++?

    Hey it is very simple to show output on both screen and file in c++. You have to just use following code to dot this. In the following code I have use ifstream and ofstream class to do this process.

    Code:
    ifstream inFile("cars.txt");
    ofstream outFile(“outputEg.txt”);
    inFile.tie(&outFile);
    
    outFile << "How is going man";
    cout <<val;
    
    outFile << "Welcome to this world ";cout <<val;

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to show output on both screen and file in c++?

    I have written following program to you. In the following code I have use ofstream class to show output on your console screen and I also have use outFile to show output on file.

    Code:
    int k = 10;
    ofstream outFile("outputEg.txt");
    
    outFile << k; // put on file
    cout << k; // put on screen

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to show output on both screen and file in c++?

    You have to use streambuf class to show output on both screen and file in c++. You have to use two methods to show output on both screen and file like outFile and cout. From these two methods outFile is used to put outPut on file and you can use cout method to show output on console screen. Just look at following code.

    Code:
    int k = 10;
    ofstream outFile("output.txt");
    
    // you have to use  some configuration on streambuf of outFile and cout
    
    cout << k;

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to show output on both screen and file in c++?

    As per my information you have to use overload cout method to fix this problem. If you don't know how to use overload cout method then use following steps.
    step1. You have to use single template << for supporting all types of cout <<
    step2. After that use define cout so that preprocessed will modify that for you.

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to show output on both screen and file in c++?

    I have written following program for you. Just try to understand it. In the following program I have use ofstream class to take input from the user and to show that input on screen. In the following code I also have use outFile method to store output in file. I also have use one destructor method to clear memory space.

    Code:
    class mstream
    {
      public:
      ofstream cosss;
      mstream(void);
      ~mstream(void);
    };
    
    template <class Ts>
    mstream& operator<< (mstream& sts, Ts val)
    {
      sts.coss << vasl;
      cout << vals;
      return sts;
    };
    
    
     sout << k << 0.1  << " Come to this world ";
    
     sout << endl;

Similar Threads

  1. Replies: 10
    Last Post: 09-01-2012, 07:48 AM
  2. Windows Batch file to output directory names and size to txt file
    By m2thearkus in forum Software Development
    Replies: 6
    Last Post: 16-07-2010, 12:04 AM
  3. Replies: 4
    Last Post: 11-04-2010, 03:03 AM
  4. Terminal output to a file
    By Naval in forum Operating Systems
    Replies: 3
    Last Post: 26-02-2009, 09:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,884,738.58672 seconds with 17 queries