Results 1 to 12 of 12

Thread: fstream -> stdout

  1. #1
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default fstream -> stdout

    HI all!
    I have ofstream objects.
    Some times i need write throu tihis objects into stdout.
    How i can relese that?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: fstream -> stdout

    Use std::cout. It is a stream interface to stdout (include <iostream>).

  3. #3
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: fstream -> stdout

    No no no
    i hope in code language i can explain my problem better...

    Qt Code:
    1. // global variable
    2. ofstream gl_fileLog;
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. // if program run with param - it log file path
    7. // else log must write into stdout
    8. if ( argc > 1)
    9. gl_fileLog.open( "some_name", ios::app );
    10. else
    11. //..... em now i want that in future my gl_FileLog write into stdout
    12. }
    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 14:04.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: fstream -> stdout

    My answer still stands

    Change:
    Qt Code:
    1. ofstream gl_fileLog;
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. ostream gl_fileLog;
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: fstream -> stdout

    My question also still stands too

    hm..with stdio it relized simply

    Qt Code:
    1. FILE *m_fileLog;
    2. ...
    3. if ( !m_bConsoleMode )
    4. {
    5. if ( ( m_fileLog = fopen( "/usr/local/mhat-1.0a/var/log/mhatControl.log","a" ) ) == NULL )
    6. exit( 0 );
    7. }
    8. else
    9. m_fileLog = stdout;
    10.  
    11. // write in file or in stdout
    12. 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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: fstream -> stdout

    I really answered your question already

    Qt Code:
    1. class logClass{
    2. public:
    3. logClass(ostream &str) : m_str(str){ m_str.open(ios::append); };
    4. logString(std::string &s){ m_str << s << std::endl; }
    5. protected:
    6. ostream &m_str;
    7. };
    8.  
    9. ofstream somefile("logme.txt");
    10.  
    11. void initialise(bool usestdout){
    12. logClass *log = new logClass(usestdout ? std::cout : somefile);
    13. log->logString("testing");
    14. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: fstream -> stdout

    I understand now. Ok Thanks i'll try it

  8. #8
    Join Date
    Jan 2006
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: fstream -> stdout

    A slightly different solution is to use rdbuf() to redirect the output:

    Qt Code:
    1. #include <iostream>
    2. #include <fstream>
    3.  
    4. int main(int argc, char** argv)
    5. {
    6. std::ofstream logFile;
    7.  
    8. // output will initially go to stdout
    9. std::ostream logStream(std::cout.rdbuf());
    10.  
    11. if( argc > 1 )
    12. {
    13. // open log file
    14. logFile.open("log.txt");
    15.  
    16. // redirect output to our log file
    17. logStream.rdbuf(logFile.rdbuf());
    18. }
    19.  
    20. logStream << "Hello world!" << std::endl;
    21.  
    22. return 0;
    23. }
    To copy to clipboard, switch view to plain text mode 
    Cervisia maintainer - http://cervisia.kde.org

  9. #9
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: fstream -> stdout

    Quote Originally Posted by wysota
    I really answered your question already

    Qt Code:
    1. class logClass{
    2. public:
    3. logClass(ostream &str) : m_str(str){ m_str.open(ios::append); };
    4. logString(std::string &s){ m_str << s << std::endl; }
    5. protected:
    6. ostream &m_str;
    7. };
    8.  
    9. ofstream somefile("logme.txt");
    10.  
    11. void initialise(bool usestdout){
    12. logClass *log = new logClass(usestdout ? std::cout : somefile);
    13. log->logString("testing");
    14. }
    To copy to clipboard, switch view to plain text mode 
    hoho, your code's ill-formed, you really should not initialize a reference in the initialization list
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: fstream -> stdout

    Quote Originally Posted by bood
    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?

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: fstream -> stdout

    @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?

  12. #12
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: fstream -> stdout

    Quote Originally Posted by wysota
    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...
    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.

Similar Threads

  1. String operations, printing to stdout
    By Cruz in forum Newbie
    Replies: 3
    Last Post: 20th January 2009, 16:30
  2. Stdout and redirection in linux
    By themolecule in forum Qt Programming
    Replies: 6
    Last Post: 15th January 2008, 18:06
  3. fstream open/close
    By mickey in forum General Programming
    Replies: 4
    Last Post: 13th October 2006, 13:23
  4. QTextStream capture stdout from xsltParseStylesheetFile
    By patrik08 in forum Qt Programming
    Replies: 9
    Last Post: 25th June 2006, 12:24
  5. QProcess problem in accessing stdout
    By aruna in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2006, 18:56

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.