HI all!
I have ofstream objects.
Some times i need write throu tihis objects into stdout.
How i can relese that?
HI all!
I have ofstream objects.
Some times i need write throu tihis objects into stdout.
How i can relese that?
Use std::cout. It is a stream interface to stdout (include <iostream>).
No no no
i hope in code language i can explain my problem better...
Qt Code:
// global variable ofstream gl_fileLog; int main(int argc, char *argv[]) { // if program run with param - it log file path // else log must write into stdout if ( argc > 1) gl_fileLog.open( "some_name", ios::app ); else //..... em now i want that in future my gl_FileLog write into stdout }To copy to clipboard, switch view to plain text mode
gl_fielLog i use in 5 other classes, and i dont want always write 2 variants.
Last edited by zlatko; 21st January 2006 at 13:04.
My answer still stands
Change: to
And assign cout to it if you want to log into stdout or your ofstream object otherwise (it is possible that gl_fileLog has to be a reference, I don't know how will cout behave if you attempt to copy it).
My question also still stands too
hm..with stdio it relized simply
Qt Code:
FILE *m_fileLog; ... if ( !m_bConsoleMode ) { if ( ( m_fileLog = fopen( "/usr/local/mhat-1.0a/var/log/mhatControl.log","a" ) ) == NULL ) exit( 0 ); } else m_fileLog = stdout; // write in file or in stdout fprintf(m_fileLog,"HELLO");To copy to clipboard, switch view to plain text mode
With stdio, when i print message i didnt must do any choices....with ostream i see i must do choise always...or i cant understand how i must solve it
I really answered your question already
Qt Code:
class logClass{ public: logClass(ostream &str) : m_str(str){ m_str.open(ios::append); }; logString(std::string &s){ m_str << s << std::endl; } protected: ostream &m_str; }; ofstream somefile("logme.txt"); void initialise(bool usestdout){ logClass *log = new logClass(usestdout ? std::cout : somefile); log->logString("testing"); }To copy to clipboard, switch view to plain text mode
I understand now. Ok Thanks i'll try it
A slightly different solution is to use rdbuf() to redirect the output:
Qt Code:
#include <iostream> #include <fstream> int main(int argc, char** argv) { std::ofstream logFile; // output will initially go to stdout std::ostream logStream(std::cout.rdbuf()); if( argc > 1 ) { // open log file logFile.open("log.txt"); // redirect output to our log file logStream.rdbuf(logFile.rdbuf()); } logStream << "Hello world!" << std::endl; return 0; }To copy to clipboard, switch view to plain text mode
Cervisia maintainer - http://cervisia.kde.org
hoho, your code's ill-formed, you really should not initialize a reference in the initialization listOriginally Posted by wysota
1. Users don't have the manual, and if they did, they wouldn't read it.
2. In fact, users can't read anything, and if they could, they wouldn't want to.
So what is a "healthy" way to initialise a member reference? And what is "ill" about my approach?Originally Posted by bood
@bood
AFAIK there is only and only one way to initialize member references here: in the constructor init list. If you try it in the body of the ctor it's not an initialization but an assignment! There's only a little problem - which variable are you assigning that value?
Sorry, my mistakeOriginally Posted by wysota
I misunderstood the standard...
1. Users don't have the manual, and if they did, they wouldn't read it.
2. In fact, users can't read anything, and if they could, they wouldn't want to.
Bookmarks