Results 1 to 3 of 3

Thread: reading files

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default reading files

    I have my files as input in my app (its a binary file). What i want to do is read the whole file and put it in a QVector <uchar> mydata. is there an easy way to do it in Qt?

    please help me,

    baray98

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: reading files

    You can do it with a QFile and a QDataStream:
    Qt Code:
    1. QFile f("c:\file.bin");
    2. if(!f.open(QIODevice::ReadOnly))
    3. return;
    4.  
    5. QDataStream stream(&f);
    6. char *s = new char[(int)f.size()];
    7. stream.readRawData(s, (int)f.size());
    To copy to clipboard, switch view to plain text mode 

    Now all the data is in s. Keep in mind that for big files you may get out of memory errors(new might fail) so the solution is to read the file in smaller buffers and process them sequentially.

    QVector<uchar> is not really optimal in this case so you should use a plain char array.

  3. #3
    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: reading files

    Or a QByteArray in a bit simpler way:
    Qt Code:
    1. QFile f("..");
    2. f.open(QFile::ReadOnly);
    3. QByteArray data = f.readAll();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  2. how to save sequences of text files and sound files
    By nagpalma in forum Qt Programming
    Replies: 8
    Last Post: 3rd July 2007, 00:06
  3. Replies: 5
    Last Post: 22nd September 2006, 08:04
  4. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28
  5. Visual Studio 2003 and .vcproj files
    By mbjerkne in forum General Discussion
    Replies: 2
    Last Post: 1st February 2006, 00:51

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.