Results 1 to 5 of 5

Thread: QTextStream, cout & files

  1. #1
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextStream, cout & files

    I saw an example of how to connect cout to a QTextStream object so that a file could be written to the same way as stdout. I can't remember where I saw the example. Could someone help me out?

    I'm guessing whatever the trick is would work for cin as well.

    Please & Thank You,

    Ed

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTextStream, cout & files

    Try:

    Qt Code:
    1. QTextStream standardOutputStream(stdout);
    2. QTextStream standardInputStream(stdin);
    To copy to clipboard, switch view to plain text mode 

    Now standardOutputStream works like cout
    And standardInputStream works like cin

  3. #3
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream, cout & files

    I may have asked the question badly. What you showed me will use the QTextStream object to ouptput to stdout (the monitor). What I want is for the output to go to a file.

    Here is what I have. I think you'll be able to see what I want to do.

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QTextStream>
    3. #include <QString>
    4. #include <QFile>
    5.  
    6. QTextStream cout(stdout, QIODevice::WriteOnly); //Same example showed what these are for.
    7. QTextStream cerr(stderr, QIODevice::WriteOnly); //I haven't figured out how to use them to output to a file
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QCoreApplication a(argc, argv);
    12. QString str1, str2(" fixPath");
    13. QFile inFile("fixPath.txt"), outFile("fixPath2.txt");
    14.  
    15.  
    16. if(inFile.open(QIODevice::ReadOnly) && outFile.open(QIODevice::WriteOnly))
    17. {
    18. QTextStream inFileStream(&inFile);
    19. QTextStream outFileStream(&outFile);
    20. while (not inFileStream.atEnd())
    21. {
    22. str1 = inFileStream.readLine();
    23. if (str1.contains(str2))
    24. {
    25. str1.remove(str2);
    26. outFileStream << str1 << endl;//here is where I'd like to be able to use cout
    27. } //to write to the file instead of the screen
    28. else
    29. outFileStream << str1 << endl;
    30. }
    31. inFile.close();
    32. outFile.close();
    33. }
    34.  
    35. //return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream, cout & files

    I think I might have just solved my own problem. I tried naming the QTextStream object cout instead of outFileStream. Now when I use cout<< the output goes to the file instead of the screen. I'm certain the example I saw was more involved than this. I'm not sure why if it's this simple. Is there a better way to do this?

    Thanx,

    Ed

  5. #5
    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: QTextStream, cout & files

    You mean that you want everything that goes to std::cout to really go to a file?

    I don't think QTextStream alone can do that. I guess you could dup() (or dup2()) stdout, open QFile on the duplicated descriptor, read data from it as it comes (using signals and slots) and write it all to some file although I wouldn't call that efficient. It would be much easier to use qDebug() instead of cout and use a handler that will write it to a file or simply stream the output directly to QTextStream connected to a file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt Creator where is cout in Qt Creator
    By hqking1988 in forum Qt Tools
    Replies: 8
    Last Post: 12th September 2019, 22:32
  2. where is cout
    By hqking1988 in forum General Programming
    Replies: 3
    Last Post: 22nd August 2010, 03:35
  3. error in QTextStream cout(stdout)
    By kaushik_acharya in forum Newbie
    Replies: 7
    Last Post: 9th April 2009, 13:24
  4. cout problem
    By mickey in forum General Programming
    Replies: 3
    Last Post: 2nd March 2007, 18:12
  5. How to cout in Qt ?
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2006, 00:47

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.