Results 1 to 9 of 9

Thread: can`t read a binary data!

  1. #1
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation can`t read a binary data!

    the binary file is generated from another software,
    it includes text and data, a mixed binary file,

    and i want use
    file.open(QIODevice::Readonly)
    to read it, but unsuccessfully,

    can QFile only read the binary file which generated from QFile?

    thanks for your HELP!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: can`t read a binary data!

    What error cause does QFile::errorString() give?
    (On linux: make sure you have read permissions and, of course, that the path to the file is correct.)

  3. #3
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: can`t read a binary data!

    Quote Originally Posted by caduel View Post
    What error cause does QFile::errorString() give?
    (On linux: make sure you have read permissions and, of course, that the path to the file is correct.)
    i have tested,
    Qt Code:
    1. if (file.open(QIODevice::Readonly))
    2. {
    3. QDataSteam in(&file);
    4. QString inin;
    5. in >> inin;
    6. textEdit->setPlainText(inin);
    7. }
    8.  
    9. else
    10. cerr<<file.error();
    11. QMessageBox::informaion(this, tr("OktoberFest"), tr("drink beer"), QMessageBox::ok);
    To copy to clipboard, switch view to plain text mode 
    the feedback is 5, that means The file could not be opened.

    but actually i can open it use Textpad

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: can`t read a binary data!

    print errorString() as well

  5. #5
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: can`t read a binary data!

    where can i use the errorstring()? haven`t found,

    now i can read the File successfully, but not output

    the File is consist of a section text date and then a section binary data,

    i don`t know how i can do it !

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: can`t read a binary data!

    errorString() is a method of QFile's base class QIOStream.


    What do you mean a section of text and binary?
    Using QDataStream you will read binary data.

    Your code
    Qt Code:
    1. QDataSteam in(&file);
    2. QString inin;
    3. in >> inin;
    To copy to clipboard, switch view to plain text mode 
    will not read some ascii from a (text) file, but rather the serialized binary representation of a QString which is something entirely different.
    If you read ascii text with that code, interesting but unintended things will happen.

  7. The following user says thank you to caduel for this useful post:

    blm (18th September 2008)

  8. #7
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: can`t read a binary data!

    thanks,
    I means that this File is consist of a section of Text and then a section of binay raw date.

    i can open this file with TextPad, but i can just only discern the head section (Text data), the latter is the binay raw data and i can`t discern them

    .

    I rewrite my code,
    Qt Code:
    1. if (file.open(QIODevice::Readonly))
    2. {
    3. cerr<<file.error();
    4. QDataSteam in(&file);
    5. data = new char;
    6. k = 1000;
    7. in.readRawData(data, k);
    8. cerr<<data;
    9. QString inin(data);
    10. textEdit->setPlainText(inin);
    11. delete data;
    12. }
    To copy to clipboard, switch view to plain text mode 

    i can see the feedback 0, then no more output

  9. #8
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: can`t read a binary data!

    you must use have (or find) some way to find out where the binary part starts.
    You can read that with QDataStream (if it was written by it).


    Qt Code:
    1. // untested and uncompiled
    2. if (file.open(QIODevice::Readonly))
    3. {
    4. QTextStream textinput(&file);
    5. do {
    6. QString line = textinput.readLine();
    7. if (line.isNull() && textinput.atEnd()) { cerr << "done\n"; break; }
    8. cerr << "read: " << qPrintable(line) << std::endl;
    9. if (line == "BINARY_FROM_NOW_ON") break; // this is assuming you have a line containing this text to mark the start of the binary part of your file
    10. }
    11. QDataSteam in(&file);
    12. // read binary stuff here
    13. }
    To copy to clipboard, switch view to plain text mode 


    Also, note that your code is buggy:
    you alloce a single byte (in; data = new char and then tell Qt to read a 1000 bytes into that "buffer".
    Apart from the fact that dynamic memory allocation is not needed here.
    try
    Qt Code:
    1. char buffer[1000];
    2. in.readRawData(buffer, 1000);
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to caduel for this useful post:

    blm (18th September 2008)

  11. #9
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: can`t read a binary data!

    thank u very much, caduel!

    where the binary part starts is difficult to find,

    because the text part is a variable long text (a section of commentary),

    then is die binary part,

    no Mark!

    I will try to find a good way to distinguish..

    thanks again!

Similar Threads

  1. How can I read binary data to QString?
    By zolookas in forum Newbie
    Replies: 2
    Last Post: 29th July 2008, 21:03
  2. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 14th June 2008, 00:05
  3. Read Data in formated manner
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 1st January 2008, 13:28
  4. Reading binary data
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 10:14
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 17: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.