Results 1 to 10 of 10

Thread: How to write and read from binary files

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question How to write and read from binary files

    Hello!

    I'm planning a software that will plot a graph using Qwt or OpenGL (yet not decided) and the data of the graph would be catch by reading a .bin file, but this is the first time I deal with binary files so I'm a "little" bit ignorant on how do I proceed.

    Before writing the actual software, I'm developing a second one to help me in my process of learning how to deal with binary files. This software has two QTextEdit side-by-side and have a "instant connection" between them (i.e. if one of them is edited, the other immediately should change base on what is written in the changed QTextEdit). But one of the QTextEdit is supposed to be connected with a .bin file and originally show what is written in the file in binary code (so one of the QTextEdit will show only a set of 0000 and 1111 and so on), while the other should be its translator, i.e. should show the binary code of the first QTextEdit in ASCII characters.

    Now I already noticed that to work in this project I will use QDataStream and QFile. But the problem is that I'm not familiar with QDataStream::readBytes() and readRayData(), and nether with the write functions. So for example in the beginning, when the QFile is opened and should show in on of the QTextEdit the bin data contained in the .bin file, I don't know which function to use in the place of QTextStream::readAll().

    Could somebody help me in this project? Which function do I use to read a binary code and show them in ASCII (e.g. 01000001 shows 65), how can I give a readAll() and how can I write an ASCII character and the software translate it to binary? And how can I make sure that the user will only write "0"s or "1"s in the QTextEdit for binary data, calling a warning if the user attempts to write a non-binary caracter? (i.e. try to put an ASCII char)


    Thanks!!


    Momergil



    Note: It's something like this I want to do: http://www.roubaixinteractive.com/Pl...ry_To_Text.asp
    Last edited by Momergil; 22nd November 2011 at 13:54. Reason: one more question

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: How to write and read from binary files

    Even though people talk about "binary files", nobody in his/her right mind would ever think of their contents as being ones and zeros, but what ever expression some data structure or number format might require. Do yourself a favor and stop thinking in ones and zeros and think like a programmer should -- at the highest abstraction level that your tool supports.
    Saving the files in some textual format would make them something like thousand times easier to debug when you change your code, data structures and file contents.

  3. #3
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write and read from binary files

    Hi,

    Don't do the qtextedit thing, it's just over-complicated and irrelevant.

    How do you know what is in your .bin file ? Is it generated by some datalogger or so, and do you have documentation about that ?

    First you should know how to interpret the .bin file, and then read it byte for byte and manipulate the bytes to turn them into usefull information.

    Regards,
    Marc

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

    Default Re: How to write and read from binary files

    If the files you are using as input are just being treated as a sequence of bytes then you do not need QDataStream, which is more aimed at providing an object serialisation system for Qt applications, you can just use the QIODevice interface directly.

    QString::number can convert a byte to its string representation in any base you like between 2 and 36.
    QString::toLong can do the opposite magic.

    Ultimately I am not sure how useful your experiment will be in getting to your end goal of graphing a data stream from another source.
    Last edited by ChrisW67; 23rd November 2011 at 03:31. Reason: spelling corrections

Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2010, 05:15
  2. Using QT to read and write files over bluetooth FTP
    By deano in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2009, 23:22
  3. Replies: 1
    Last Post: 27th August 2009, 05:41
  4. How can I read Binary files with Qt
    By geo_saleh in forum Qt Programming
    Replies: 2
    Last Post: 16th August 2007, 10:37
  5. How to read/write sets of orderded numbers in binary
    By kaydknight in forum General Programming
    Replies: 3
    Last Post: 12th March 2007, 05:50

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.