Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-02-2010
Member
 
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.

Reply With Quote
  #2  
Old 12-02-2010
Member
 
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;
Reply With Quote
  #3  
Old 12-02-2010
Member
 
Join Date: May 2008
Posts: 2,008
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
Reply With Quote
  #4  
Old 12-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
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;
Reply With Quote
  #5  
Old 12-02-2010
Member
 
Join Date: May 2008
Posts: 2,293
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.
Reply With Quote
  #6  
Old 12-02-2010
Member
 
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;
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to show output on both screen and file in c++?"
Thread Thread Starter Forum Replies Last Post
getting Error splicing file: Input/output error while copying file in ubuntu Rajni Operating Systems 10 09-01-2012 07:48 AM
Windows Batch file to output directory names and size to txt file m2thearkus Software Development 6 16-07-2010 12:04 AM
Toshiba Satellite A205-S5803 Black Screen. Does not load login screen or show mouse pointer Nereus Portable Devices 4 11-04-2010 03:03 AM
Terminal output to a file Naval Operating Systems 3 26-02-2009 09:00 PM


All times are GMT +5.5. The time now is 04:02 PM.