Results 1 to 2 of 2

Thread: Read/Write console app

  1. #1
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Read/Write console app

    Hello,

    I'm quite new to QT. I decided to start learning it after learning to do some console app programming. I'm still going through tutorials and what not. I've got a bit of code here, and I'm not sure what needs fixing.

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QDebug>
    3. #include <QString>
    4. #include <QFile>
    5. #include <QTextStream>
    6. #include <iostream>
    7.  
    8. using namespace std;
    9.  
    10. void Write(QString filename);
    11. void Read (QString filename);
    12.  
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16. QCoreApplication a(argc, argv);
    17.  
    18. QString filename = "C:/myfile.txt";
    19.  
    20. Write(filename);
    21. Read(filename);
    22.  
    23. return a.exec();
    24. }
    25.  
    26. void Write(QString filename){
    27.  
    28. QFile mFile(filename);
    29.  
    30. if (!mFile.open(QFile::WriteOnly | QFile::Text )){
    31.  
    32. cout << "could not write file.";
    33. return;
    34.  
    35. }
    36. QTextStream out(&mFile);
    37. cout << "hello world!!";
    38. mFile.flush();
    39. mFile.close();
    40. }
    41.  
    42. void Read(QString filename){
    43.  
    44. QFile mFile(filename);
    45. if (!mFile.open(QFile::ReadOnly | QFile::Text )){
    46.  
    47. cout << "could not open file for reading.";
    48. return;
    49.  
    50. }
    51. QTextStream in(&mFile);
    52. QString mtext = in.readAll();
    53. QDebug << mtext;
    54.  
    55. in << "hello world!!";
    56. mFile.close();
    57.  
    58. }
    To copy to clipboard, switch view to plain text mode 

    the error is showing up on line 53.

    error: expected unqualified-id before '<<' token

  2. #2
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Read/Write console app

    Qt Code:
    1. qDebug() << mtext;
    To copy to clipboard, switch view to plain text mode 
    Oleg Shparber

Similar Threads

  1. Replies: 7
    Last Post: 13th September 2011, 13:15
  2. Replies: 2
    Last Post: 2nd November 2010, 05:15
  3. How to write qt4 gui up to the existing console
    By binaural in forum Qt Programming
    Replies: 4
    Last Post: 16th June 2009, 13:44
  4. read and write on qtcpsocket
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 09:42
  5. XML -read-write
    By hgedek in forum Qt Programming
    Replies: 2
    Last Post: 3rd September 2007, 18:10

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.