Results 1 to 4 of 4

Thread: How to read/write sets of orderded numbers in binary

  1. #1
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to read/write sets of orderded numbers in binary

    Hello, I would like to ask a question.

    Does anyone know how to effectively store a 2d array of numbers in binary file, and then able to read them later into array.

    For example, This are the samples of numbers I need to write:

    x1 x2 x3
    y1 1 1 1
    y2 2 2 3
    y3 3 3 5
    y4 4 5 6
    y5 5 6 8

    If in ascii, I can write like this:
    1 1 1
    2 2 2
    3 3 5
    ...
    5 6 8

    But how about writing it into binary file?
    When reading, How do I know when I reached end-of-row for one column and store next number on column+1 (since binary files are ordered sequentially).
    I've been thinking of putting some kind of byte signal to indicate I reached end-of-row, but I don't have the idea how.
    Also, I predict a problem, where later, my sets of number will be non-uniformed (1 col have 5 rows, next col have 9 rows and so on).

    Can anyone give a simple example how to write it? I'm comfortable with using QDataStream and iostreams binary mode, but I think this is more a problem of the design of it.

    E.g : After reading the binary file, I want to store them into array.
    array[0,0] = 1
    ... and so on until
    array[2,4] = 8

    Thanks in advance! I've tried googling, but they only tell me the basics of binary read/write.
    Last edited by kaydknight; 9th March 2007 at 04:45.

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read/write sets of orderded numbers in binary

    The easiest would be to store the dimensions to expect first, for example, this matrix:

    1 1 1
    2 2 2
    3 3 3

    Would be stored as

    3 3 1 1 1 2 2 2 3 3 3

    Where the first two threes would indicate that we're dealing with a 3x3 matrix. Then the data follow. Using an index, starting at 0 for the first one, then increasing you can find your matrix coordinate like this:

    row = index/width; // Integer division, always rounds down
    col = index%width; // Modulus

    The final check would be to ensure that the index == width*height when you have reached the end of the file.

    Also, when storing binary data using Qt, make sure to set your QDataStream version using myDataStreamObject->setVersion( xxx ); to ensure compatibility between Qt versions.

  3. The following user says thank you to e8johan for this useful post:

    kaydknight (10th March 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to read/write sets of orderded numbers in binary

    Oh, what a solution! Thanks a lot for this! I'm baffled on how I can't come up with such a cheap (meaning not computing expensive) solution. I guess this seperates programmers, those who can program, and those who can design software.

    As I said, I originally tried to represent a byte as end of row. Well to do it, I must check every designated byte for it, using for and if loops, that would have really slow down computing time.

    Emm.. just a general question then, is this how people code for binary data, by inputting the sizes of particular row/col first? Are there any other types out there? I'm curious on the designs of binary read/write people can come up with.
    Last edited by kaydknight; 10th March 2007 at 19:03.

  5. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read/write sets of orderded numbers in binary

    Quote Originally Posted by e8johan View Post
    The easiest would be to store the dimensions to expect first, for example, this matrix:

    1 1 1
    2 2 2
    3 3 3

    Would be stored as

    3 3 1 1 1 2 2 2 3 3 3

    Where the first two threes would indicate that we're dealing with a 3x3 matrix. Then the data follow. Using an index, starting at 0 for the first one, then increasing you can find your matrix coordinate like this:

    row = index/width; // Integer division, always rounds down
    col = index%width; // Modulus

    The final check would be to ensure that the index == width*height when you have reached the end of the file.

    Also, when storing binary data using Qt, make sure to set your QDataStream version using myDataStreamObject->setVersion( xxx ); to ensure compatibility between Qt versions.
    Hi e8johan,
    I have written a sample program about what you have told in above post. Please correct me if I'm wrong...

    Qt Code:
    1. #include <iostream>
    2. #include <fstream>
    3. using namespace std;
    4.  
    5. int main()
    6. {
    7. int arr[3][3] = { 1, 1, 1,
    8. 2, 2, 2,
    9. 3, 3, 3 };
    10.  
    11.  
    12. ofstream ofs("test.txt", ios::binary);
    13. const char* data = "33111222333";
    14. string line = data;
    15. ofs.write( line.data(), line.length() );
    16. ofs.close();
    17.  
    18. char buffer[256];
    19. ifstream ifs("test.txt", ios::binary);
    20. ifs.getline(buffer, sizeof(buffer) );
    21. ifs.close();
    22.  
    23. string bufferdata = buffer;
    24.  
    25. int width = bufferdata.at(0) - '0';
    26. int height = bufferdata.at(1) - '0';
    27.  
    28. int index = 0;
    29. int row = 0, col = 0;
    30.  
    31. while(1)
    32. {
    33. if( index == width*height )
    34. break;
    35.  
    36. row = index/width; // Integer division, always rounds down
    37. col = index%width; // Modulus
    38.  
    39. cout<<"Row:"<<row<<" "<<"Col:"<<col<<endl;
    40. bufferdata.at(index++);
    41. }
    42. return 0;
    43. }
    To copy to clipboard, switch view to plain text mode 

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.