Results 1 to 10 of 10

Thread: writing huge binary data

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default writing huge binary data

    Hi,
    there is a 2D array 500*100,000 like this:

    Qt Code:
    1. double (*data)[100000];
    2. data = new double[500][100000];
    3.  
    4. for(int i=0; i<500; i++)
    5. {
    6. for(int j=0; j<100000; j++)
    7. {
    8. if(j==0)
    9. {
    10. data[i][j]=0xdd00bb00;//i want this to be my header
    11. continue;
    12. }
    13. data[i][j] = (i+1)*(j+1);
    14. }
    15. }
    16. // now data was initialized
    To copy to clipboard, switch view to plain text mode 

    I wrote this code:

    Qt Code:
    1. void write(QString filename)
    2. {
    3. QFile myfile(filename);
    4.  
    5. if(!myfile.open(QIODevice::WriteOnly))
    6. {
    7. qDebug()<<"can not open to write";
    8. return;
    9. }
    10.  
    11. QDataStream out(&myfile);
    12.  
    13. //now I want to write all of "data" in binary mode
    14.  
    15. myfile.flush();
    16. myfile.close();
    17. }
    To copy to clipboard, switch view to plain text mode 

    thanks for any help

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: writing huge binary data

    Well, if you don't have any format constraints, just loop over the entries and write them.
    Maybe write the dimensions before, so you can read them before reading the data, unless your data has defined fixed size.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: writing huge binary data

    please read under post (sorry because of slow speed of the sit post was been repeated)!!!
    Last edited by Alex22; 12th December 2015 at 11:53.

  4. #4
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: writing huge binary data

    @anda_skoa thanks. when i use this to write, it gives a filename.txt in zero size:

    Qt Code:
    1. double (*data)[100000];
    2. data = new double[500][100000];
    3.  
    4. for(int i=0; i<500; i++)
    5. {
    6. for(int j=0; j<100000; j++)
    7. {
    8. if(j==0)
    9. {
    10. data[i][j]=0xdd00bb00;//i want this to be my header
    11. continue;
    12. }
    13. data[i][j] = (i+1)*(j+1);
    14. }
    15. }
    16. // now data was initialized
    17.  
    18. QFile myfile("d:/filename.txt");
    19.  
    20. if(!myfile.open(QIODevice::WriteOnly))
    21. {
    22. qDebug()<<"can not open to write";
    23. return;
    24. }
    25.  
    26. QDataStream out(&myfile);
    27.  
    28. out.writeRawData((char *) (&(data[0][0])), 100000*500*sizeof(double)); // did you mean i write here??? but .txt size is zero and nothing is written
    29.  
    30. myfile.flush();
    31. myfile.close();
    To copy to clipboard, switch view to plain text mode 

    thanks for any help
    Last edited by Alex22; 12th December 2015 at 11:47.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: writing huge binary data

    Well, since you indicated usage of QDataStream, I assumed you wanted to use it, so I meant looping through data and writing the doubles.

    Now the question is, why instantiate a QDataStream if you are then bypassing it?
    If you want to go down the route of doing a memory dump, why not just use QFile directly?

    In any case, have you checked the return values of the operations? Also what size does QFile report after writing and after closing?

    Cheers,
    _

    P.S.: naming a binary file ".txt" doesn't make sense.

  6. #6
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: writing huge binary data

    @anda_skoa, thanks. I found an amazing thing happens here. for data[m][n] (double (*data)[n]; data = double[m][n]) when m>n it gives me right myfile.txt but when m<n it gives a file in size of zero!!! for example when m=599 and n=600 it gives me a file in size of zero and returns:"The program has unexpectedly finished." but when m=601 and n=600 it gives me a file in size of 2,344kB

    even when m is so larger than n, there is no false but if n is only one bigger that m, its not ok!!!

    for example:
    Qt Code:
    1. #include <QApplication>
    2. #include <QFile>
    3. #include <QDebug>
    4. #include <QDataStream>
    5.  
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10.  
    11. QFile myfile("d:/test.txt");
    12.  
    13. if(!myfile.open(QIODevice::WriteOnly))
    14. {
    15. qDebug() << "can not open to write";
    16. return 1;
    17. }
    18.  
    19. double (*data)[600];
    20. data = new double[599][600];//now is not ok but if i define data = new double[601][600] it gives me right file!!!
    21.  
    22. for(int i=0; i<599; i++)
    23. {
    24. for(int j=0; i<600; i++)
    25. {
    26. data[i][j] = (i+1)*(j+1);
    27. }
    28. myfile.write((char *)(&(data[i][600])), sizeof(double));
    29. }
    30.  
    31. //myfile.write((char *)(&(data[0][0])), 599*600*sizeof(double)); when m>n this is true also
    32.  
    33. myfile.flush();
    34. myfile.close();
    35.  
    36. return 0;
    37. }
    To copy to clipboard, switch view to plain text mode 

    You are right I need no QDataStrem and I can use QFile directly.

    thanks for anymore help
    Last edited by Alex22; 12th December 2015 at 14:33.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: writing huge binary data

    and returns:"The program has unexpectedly finished."
    Ah, no.
    It crashed.

    You could run the program in the debugger and look where it crashes.

    I would suggest to either use a one dimensional array and calculate the offset based on i and j or initialize each entry in your array of rows individually.

    Cheers,
    _

  8. #8
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: writing huge binary data

    Thanks Anda,
    I wrote only in one row and now it gives a file in size of 390,625kB (for below example), but almost every byte inside of this file is zero. my code is:

    Qt Code:
    1. #include <QApplication>
    2. #include <QFile>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. QFile myfile("d:/test.txt");
    10.  
    11. if(!myfile.open(QIODevice::WriteOnly))
    12. {
    13. qDebug() << "can not open to write";
    14. return 1;
    15. }
    16.  
    17. double *d;
    18. d = new double[500*100000];
    19.  
    20. int i = 0;
    21. while(i<100000)
    22. {
    23. for(int j=0; j<500; j++)
    24. {
    25. d[j]=(i+1)*(j+1);
    26. }
    27. i = i + 500;
    28. }
    29.  
    30. myfile.write((char *) d, 500*100000*sizeof(double));
    31. myfile.flush();
    32. myfile.close();
    33.  
    34. return 0;
    35. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your helping
    Last edited by Alex22; 12th December 2015 at 15:39.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: writing huge binary data

    You are always writing to the first 500 doubles.

    Cheers,
    _

  10. #10
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: writing huge binary data

    @anda_sko, sorry I must write [j+(i*500)] and i++. now all thing is OK and i could write from a 2D array to a text.
    Last edited by Alex22; 12th December 2015 at 19:58.

Similar Threads

  1. qt binary file writing and reading
    By seniorc in forum Newbie
    Replies: 9
    Last Post: 17th December 2013, 23:03
  2. How to handle huge data with QSqlQueryModel?
    By alizadeh91 in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2013, 15:31
  3. Writing my own object to a binary file.
    By 8Observer8 in forum Newbie
    Replies: 1
    Last Post: 1st December 2013, 08:15
  4. How to store huge data in a Qt software
    By Momergil in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2012, 22:43
  5. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 19:24

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.