Results 1 to 20 of 37

Thread: Can somebody show how to read bytes?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Can somebody show how to read bytes?

    Well,I have tried many different combination with different syntax...I cannot post a certain code,as there were many things I tried..

    Can you please post a working piece?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can somebody show how to read bytes?

    Post the the code you thought was the best of the things you tried, and we can pick it up from there.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Can somebody show how to read bytes?

    Sure.
    Well I made this:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QFile>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. QFile f("C:\\Program Files (x86)\\TeamViewer\\Version6\\TeamViewer.exe");
    10. if( !f.exists() )
    11.  
    12. {
    13. // It does not exist
    14. qDebug() << "The file does not exist.\n";
    15. }
    16.  
    17. // It exists, open it
    18. if(!f.open(QIODevice::ReadOnly))
    19. {
    20. // It could not open
    21. qDebug() << "Failed to open.";
    22.  
    23. return 0;
    24. }
    25.  
    26. // It opened, now we need to close it
    27. qDebug() << "Success";
    28.  
    29. int size = f.size();
    30. qDebug() << "size: " << size;
    31.  
    32. QDataStream data(&f);
    33. char * buffer;
    34. uint u = sizeof(f);
    35. data.readBytes(buffer,u);
    36.  
    37.  
    38. f.close();
    39. return a.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    Then I thought that I may need to make a loop,in order to read a byte after byte..but still nothing.

  4. #4
    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: Can somebody show how to read bytes?

    The code doesn't make much sense really. Why don't you just use QIODevice::read() on your QFile object?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can somebody show how to read bytes?

    Sorry,

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. QFile f("C:\\Program Files (x86)\\TeamViewer\\Version6\\TeamViewer.exe");
    6.  
    7. ....
    8. int size = f.size();
    9. qDebug() << "size: " << size;
    10.  
    11. QDataStream data(&f);
    12. char * buffer;
    13. uint u = sizeof(f);
    14. data.readBytes(buffer,u);
    15.  
    16.  
    17. f.close();
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    what you're doing??

    1. You're using a pointer "buffer" as array of bytes (WRONG)
    2. You're using sizeof of an object as amount of data to read (WRONG)


    for 1. it's strange that your program doesn't crash

    I suggest you

    Qt Code:
    1. int size = f.size();
    2. qDebug() << "size: " << size;
    3.  
    4. QByteArray buffer = f.read (size);
    To copy to clipboard, switch view to plain text mode 

    and use directly "buffer"
    Last edited by mcosta; 19th April 2011 at 16:07. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 2
    Last Post: 9th June 2010, 16:08
  2. How to read only a certain amount of bytes
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 07:38
  3. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 4th July 2007, 23:12
  4. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 20:23
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 14:12

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.