Results 1 to 3 of 3

Thread: QFile not working with >>

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question QFile not working with >>

    Hi,

    I'm trying to use QFile to open a text file (floating numbers in a single column) and read the numbers. The example below won't even compile, something is wrong with it but I don't know what.

    Qt Code:
    1. QFile INfile("example1.txt");
    2. if(!INfile.open(QIODevice::ReadOnly | QIODevice::Text)) {exit(1);}
    3. float numb;
    4.  
    5. while (!INfile.atEnd())
    6. {
    7. INfile >> numb;
    8. cout << numb << "\n";
    9. }
    10.  
    11. INfile.close();
    To copy to clipboard, switch view to plain text mode 

    Thanks

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QFile not working with >>

    Use QTextStream to read from text files:
    Qt Code:
    1. QFile INfile("example1.txt");
    2. if(!INfile.open(QIODevice::ReadOnly | QIODevice::Text)) {exit(1);}
    3. float numb;
    4. QTextStream stream(&INfile);
    5. while (!stream.atEnd())
    6. {
    7. stream >> numb;
    8. cout << numb << "\n";
    9. }
    10. INfile.close();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to stampede for this useful post:

    timmu (30th December 2011)

  4. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile not working with >>

    op, docs are here:
    http://developer.qt.nokia.com/doc/qt-4.8/qfile.html

    and they don't mention anything about operator >>.

    QTextStream on the other hand...
    http://developer.qt.nokia.com/doc/qt...extstream.html


    The Qt docs are very good. Your compiler, I'm sure, will have given a quite obvious error. Part of being a programmer is being able to solve small issues like this. If you put no effort into learning how to resolve these trivial issues, you will find yourself posting, and waiting, and wasting lots and lots of time.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. QFile::Seek() not working
    By derrickbj in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2011, 20:14
  2. Replies: 4
    Last Post: 9th May 2011, 09:52
  3. QFile &QFile::operator= is private
    By Fallen_ in forum Newbie
    Replies: 1
    Last Post: 15th March 2011, 15:08
  4. QFile map
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2011, 08:17
  5. QFile using utf-8
    By Mr.QT in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2009, 12:17

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.