PDA

View Full Version : fstream -> stdout



zlatko
21st January 2006, 11:37
HI all!
I have ofstream objects.
Some times i need write throu tihis objects into stdout.
How i can relese that?

wysota
21st January 2006, 11:48
Use std::cout. It is a stream interface to stdout (include <iostream>).

zlatko
21st January 2006, 14:02
No no no:)
i hope in code language i can explain my problem better...:rolleyes:



// 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
}


gl_fielLog i use in 5 other classes, and i dont want always write 2 variants.

wysota
21st January 2006, 14:07
My answer still stands :)

Change:
ofstream gl_fileLog; to
ostream gl_fileLog;

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).

zlatko
21st January 2006, 14:39
My question also still stands too:confused:

hm..with stdio it relized simply



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");


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:(

wysota
21st January 2006, 17:03
I really answered your question already :)


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");
}

zlatko
21st January 2006, 17:22
I understand now. Ok Thanks i'll try it:)

cloose
23rd January 2006, 14:22
A slightly different solution is to use rdbuf() to redirect the output:



#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;
}

bood
24th January 2006, 16:18
I really answered your question already :)


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");
}

hoho, your code's ill-formed, you really should not initialize a reference in the initialization list:)

wysota
24th January 2006, 16:46
hoho, your code's ill-formed, you really should not initialize a reference in the initialization list:)

So what is a "healthy" way to initialise a member reference? And what is "ill" about my approach?

Codepoet
24th January 2006, 19:39
@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? ;)

bood
25th January 2006, 06:15
So what is a "healthy" way to initialise a member reference? And what is "ill" about my approach?

Sorry, my mistake
I misunderstood the standard...:(