Results 1 to 8 of 8

Thread: Reading all from a file that contains "\0" character

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    866
    Thanks
    69
    Thanked 58 Times in 56 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Reading all from a file that contains "\0" character

    Hi,

    I'm reading a text file with QFile that may contain "\0" characters.
    The problem is that QFile::read() returns only first characters to the first "\0" occurence.

    Anyone know how to read all the characters from the file to the end?

    Thanks,
    Òscar Llarch i Galán

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

    Default Re: Reading all from a file that contains "\0" character

    Try with QDataStream :: readRawData:
    Qt Code:
    1. QFile file("test.txt");
    2. if( file.open(QIODevice::WriteOnly) ){
    3. file.putChar('a');
    4. file.putChar('\0');
    5. file.putChar('b');
    6. file.close();
    7. if( file.open(QIODevice::ReadOnly) ){
    8. const int size = file.size();
    9. QByteArray ba(size,'x');
    10. QDataStream stream(&file);
    11. stream.readRawData(ba.data(),size);
    12. qDebug() << ba.length();
    13. if( ba.length() > 2 ){
    14. qDebug() << (ba[0] == 'a') << (ba[1] == '\0') << (ba[2] == 'b');
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Looks like it works ok for me, output is:
    warning: 3
    warning: true true true

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    866
    Thanks
    69
    Thanked 58 Times in 56 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading all from a file that contains "\0" character

    Hi,

    I've used "QQFile:eek" that does the job.

    Now the problem is that the QByteArray that contains the data shows that only contains data until the "\0" character but it's size is larger. I need to use all the data readed and when unsing "QByteArray::mid" it only gets the text untill "\0".

    Thanks,
    Òscar Llarch i Galán

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,593
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Reading all from a file that contains "\0" character

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    I'm reading a text file with QFile that may contain "\0" characters.
    The problem is that QFile::read() returns only first characters to the first "\0" occurence.
    No it doesn't. QIODevice::read() reads up to the specified number of bytes from the stream, in this case a file. It doesn't care what the bytes are.

    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv) {
    5. QCoreApplication app(argc, argv);
    6.  
    7. // Write a test file
    8. QByteArray data("ABCD\0FGHI\0JK", 12);
    9. QFile test("test.bin");
    10. if (test.open(QIODevice::WriteOnly)) {
    11. qint64 wrote = test.write(data);
    12. qDebug() << "Wrote" << wrote << "of" << data.size() << "bytes";
    13. test.close();
    14. }
    15.  
    16. // Now read it back
    17. if (test.open(QIODevice::ReadOnly)) {
    18. QByteArray result = test.readAll();
    19. qDebug() << "readAll() == " << result.size() << "bytes";
    20.  
    21. test.seek(0);
    22. result = test.read(1000);
    23. qDebug() << "read() == " << result.size() << "bytes";
    24.  
    25. test.close();
    26. }
    27.  
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    Qt Code:
    1. Wrote 12 of 12 bytes
    2. readAll() == 12 bytes
    3. read() == 12 bytes
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading all from a file that contains "\0" character

    Hi,
    I think the simple method to solve your problem if your input file is too big is use this step:
    1. get size of your input file (use fseek)
    2. set size buffer to read file (nbuffer). You can set nbuffer=1 if you want to read your input file every 1 byte, nbuffer=200 read every 200 byte, etc.
    3. read file with size nbuffer.
    4. check buffer. loop from 0 to nbuffer-1. If you dont get "\0" charater, go to number 3. If you get "\0" chacracter, exit loop and get "\0" location.

    I use this method when reading a big file binary data (more than 6Gb) and success.
    I think this is simple. Are you think this is solve your problem?

    Best regards,

    Myta

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    866
    Thanks
    69
    Thanked 58 Times in 56 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading all from a file that contains "\0" character

    Hi,

    Thanks all, I've solved my problem.
    Òscar Llarch i Galán

  7. #7
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reading all from a file that contains "\0" character

    If you can, post your solution so other can solve their problems when they get across this thread.
    Cheers!

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    866
    Thanks
    69
    Thanked 58 Times in 56 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading all from a file that contains "\0" character

    Hi,

    As ChrisW67 reported, it reads all the bytes. The problem was on using this into a QByteArray or a QString object as this classes use "\0" as end of string.
    So here I had the problem. As I'm who is writing the file content, I just changed the way that the data is stored. I used QByteArray::toBase64 before writing to the file.

    So, Spitfire there is no really a solution to post.
    Òscar Llarch i Galán

Similar Threads

  1. Replies: 10
    Last Post: 17th July 2014, 10:52
  2. Replies: 3
    Last Post: 15th February 2010, 17:27
  3. Replies: 0
    Last Post: 9th July 2008, 22:32
  4. Replies: 3
    Last Post: 8th July 2008, 19:37
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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.