|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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; |
![]() |
|
Tags: c language, class, file handle, function, object |
Thread Tools | Search this Thread |
|
![]() | ||||
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 |