Results 1 to 14 of 14

Thread: Concatenating two binary files

  1. #1
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Concatenating two binary files

    I want to concatenate two binary files.

    Basically there is a header file containing a Print Job ticket and a PDF file. I want to put the two together..

    Here is what I have..

    Qt Code:
    1. QFile file("out.prn");
    2. if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    3. return;
    To copy to clipboard, switch view to plain text mode 

    The Ticket is obtained and opened read only...

    Qt Code:
    1. QString ticketName = ticketComboBox->currentText();
    2. QFile *ticket = new QFile(ticketName, ftp);
    3. ticket->open(QIODevice::ReadOnly);
    4. QFileInfo ti( ticketName );
    To copy to clipboard, switch view to plain text mode 

    Then the PDF file...
    Qt Code:
    1. QString fileName = directoryComboBox->currentText();
    2. QFile *file = new QFile(fileName, ftp);
    3. file->open(QIODevice::ReadOnly);
    4. QFileInfo fi( fileName );
    To copy to clipboard, switch view to plain text mode 

    My problem is the connection from the Open files and how to dump them into the newly created "out.prn"

    Ideas?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenating two binary files

    Don't forget that QFile inherits QIODevice:

    Qt Code:
    1. QFile prnFile;
    2. //
    3. //...Create the output file, etc
    4. //
    5.  
    6. //first write the ticket
    7. qint64 totalSize = ticket->size();
    8. qint64 chunkSize = 2048; //you can increase it and decrease it as you will
    9.  
    10. char *readBuffer = new char[chunkSize];
    11. while ( totalSize )
    12. {
    13. if( chunkSize > totalSize )
    14. chunkSize = totalSize;
    15. qint64 actuallyRead = ticket->read( readBuffer, chunkSize );
    16.  
    17. if( actuallyRead > 0 )
    18. prnFile->write( readBuffer, actuallyRead );
    19. else
    20. break;
    21.  
    22. totalSize -= actuallyRead;
    23. }
    24. // And when you're done:
    25. delete[] readBuffer;
    26. outPrn.flush();
    To copy to clipboard, switch view to plain text mode 
    This is only to write the ticket.
    After this you can write the same code to write the pdf file( you must use the same output file, and to make sure, position the file pointer at the end of the file ). Or you can make it a function and call it twice.

    For more information you can check QIODevice class description.
    Feel free to ask if you have any other questions...

    Regards
    Last edited by marcel; 20th April 2007 at 17:36. Reason: took something out of while loop :)

  3. #3
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Concatenating two binary files

    Marel,

    Thanks for the pointers this is great.

    Im getting an error on running (compiles ok), out.prn is created, but nothing is in it...

    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    2. QObject: Cannot create children for a parent that is in a different thread.
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // The Two Files are here.
    2. QString ticketName = ticketComboBox->currentText();
    3. QFile *ticket = new QFile(ticketName, ftp);
    4. ticket->open(QIODevice::ReadOnly);
    5. QFileInfo ti( ticketName );
    6.  
    7. QString fileName = directoryComboBox->currentText();
    8. QFile *file = new QFile(fileName, ftp);
    9. file->open(QIODevice::ReadOnly);
    10. QFileInfo fi( fileName );
    11.  
    12. // now put the two files together.
    13.  
    14.  
    15.  
    16. QFile prnFile;
    17.  
    18. QFile outPrn("out.prn");
    19. if (!outPrn.open(QIODevice::WriteOnly))
    20. return;
    21.  
    22. //first write the ticket
    23.  
    24. qint64 totalSize = ticket->size();
    25. qint64 chunkSize = 2048; //you can increase it and decrease it as you will
    26.  
    27. char *readBuffer = new char[chunkSize];
    28. while ( totalSize )
    29. {
    30. if( chunkSize > totalSize )
    31. chunkSize = totalSize;
    32. qint64 actuallyRead = ticket->read( readBuffer, chunkSize );
    33.  
    34. if( actuallyRead > 0 )
    35. prnFile.write( readBuffer, actuallyRead );
    36. else
    37. break;
    38. totalSize -= actuallyRead;
    39. // And when you're done:
    40. delete[] readBuffer;
    41. outPrn.flush();
    42. }
    43.  
    44. //Now Write the PDF file
    45.  
    46. qint64 ptotalSize = file->size();
    47. qint64 pchunkSize = 2048; //you can increase it and decrease it as you will
    48.  
    49. char *preadBuffer = new char[pchunkSize];
    50. while ( ptotalSize )
    51. {
    52. if( pchunkSize > ptotalSize )
    53. pchunkSize = ptotalSize;
    54. qint64 pactuallyRead = file->read( preadBuffer, pchunkSize );
    55.  
    56. if( pactuallyRead > 0 )
    57. prnFile.write( preadBuffer, pactuallyRead );
    58. else
    59. break;
    60. ptotalSize -= pactuallyRead;
    61. // And when you're done:
    62. delete[] preadBuffer;
    63. outPrn.flush();
    64. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenating two binary files

    Quote Originally Posted by nbkhwjm View Post
    Im getting an error on running (compiles ok), out.prn is created, but nothing is in it...

    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    2. QObject: Cannot create children for a parent that is in a different thread.
    To copy to clipboard, switch view to plain text mode 
    Are you running this code within a thread (and especially in the run() method?) If so then you need take care of the parent of you QIODevice... This is a really tricky problem but passing a null pointer to the constructor should do...
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenating two binary files

    Before everything, check your SRC !!!
    Take that delete out of the while! The read buffer should be deleted only one, at the end, when you're done with it.

    Regards

  6. #6
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Concatenating two binary files

    This function is called as a response to a slot/signal from a SEND button.. The apps comes up , but throws the errors... and now an additonal one...

    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    2. QObject: Cannot create children for a parent that is in a different thread.
    3. *** glibc detected *** double free or corruption (!prev): 0x08b8ad00 ***
    4. *** Process aborted ***
    To copy to clipboard, switch view to plain text mode 

    here is the full function...

    Qt Code:
    1. void Window::sendFile()
    2. {
    3.  
    4. cancelButton->setEnabled(true);
    5. sendButton->setEnabled(false);
    6. quitButton->setEnabled(false);
    7. browseButton->setEnabled(false);
    8. protocolComboBox->setEnabled(false);
    9. directoryComboBox->setEnabled(false);
    10. printerComboBox->setEnabled(false);
    11.  
    12. // The Two Files are here.
    13. QString ticketName = ticketComboBox->currentText();
    14. QFile *ticket = new QFile(ticketName, ftp);
    15. ticket->open(QIODevice::ReadOnly);
    16. QFileInfo ti( ticketName );
    17.  
    18. QString fileName = directoryComboBox->currentText();
    19. QFile *file = new QFile(fileName, ftp);
    20. file->open(QIODevice::ReadOnly);
    21. QFileInfo fi( fileName );
    22.  
    23. // now put the two files together.
    24. QFile outPrn("out.prn");
    25.  
    26. //first write the ticket
    27. qint64 totalSize = ticket->size();
    28. qint64 chunkSize = 2048;
    29.  
    30. char *readBuffer = new char[chunkSize];
    31. while ( totalSize )
    32. {
    33. if( chunkSize > totalSize )
    34. chunkSize = totalSize;
    35. qint64 actuallyRead = ticket->read( readBuffer, chunkSize );
    36.  
    37. if( actuallyRead > 0 )
    38. outPrn.write( readBuffer, actuallyRead );
    39. else
    40. break;
    41. totalSize -= actuallyRead;
    42.  
    43. delete[] readBuffer;
    44. outPrn.flush();
    45. }
    46.  
    47. qint64 ptotalSize = file->size();
    48. qint64 pchunkSize = 2048;
    49.  
    50. char *preadBuffer = new char[pchunkSize];
    51. while ( ptotalSize )
    52. {
    53. if( pchunkSize > ptotalSize )
    54. pchunkSize = ptotalSize;
    55. qint64 pactuallyRead = file->read( preadBuffer, pchunkSize );
    56.  
    57. if( pactuallyRead > 0 )
    58. outPrn.write( preadBuffer, pactuallyRead );
    59. else
    60. break;
    61. ptotalSize -= pactuallyRead;
    62. delete[] preadBuffer;
    63. outPrn.flush();
    64. }
    65.  
    66.  
    67.  
    68.  
    69. ftp = new QFtp(this);
    70.  
    71. connect(ftp, SIGNAL(stateChanged(int)),
    72. this, SLOT(ftp_stateChanged(int)));
    73.  
    74. connect(ftp, SIGNAL(commandFinished(int, bool)),
    75. this, SLOT(ftpCommandFinished(int, bool)));
    76.  
    77.  
    78. // FTP the final File to the printer here.
    79.  
    80. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenating two binary files

    Why do you create the QFiles with that ftp object as parent?
    Did you know that ftp is not created when you pass it as parent? This is one problem.

    As for the crash, I already said, delete the read buffer when you're done with it completely, meaning after you finished copying the pdf file.

    I'm sure you get the thread error because ftp has an invalid address when you create the QFiles.

    Solution: don't pass the ftp object bas parent to the QFiles. Pass NULL.

    Regards
    Last edited by marcel; 20th April 2007 at 20:45.

  8. #8
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Concatenating two binary files

    I started with an application that would FTP one file to the printer. This worked great..

    Then I needed to send two file... so I added a second, but it turned out that i needed to send both files as one...

    so looks like i got it...

    I appreciate the patience... the issue seems big until you see the problem...

    any way this error doesnt seem to cause the app to fail...

    QObject: Cannot create children for a parent that is in a different thread.

    should i ignore or keep working though it?

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenating two binary files

    any way this error doesnt seem to cause the app to fail...

    QObject: Cannot create children for a parent that is in a different thread.

    should i ignore or keep working though it?
    I already told you...Pass NULL instead of ftp when you create the QFiles and you will not get this error.

    You get this error because the ftp pointer has an invalid adress when you create the QFiles.
    At least initialize the pointer to NULL at the beginning of the application.

    Regards

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

    nbkhwjm (20th April 2007)

  11. #10
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Concatenating two binary files

    Marcel,

    I completely missed that point, got it....

    Thank you very much!!

  12. #11
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Concatenating two binary files

    as a Noob to C++, I appreciate your assistance very much!!

  13. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenating two binary files

    You're welcome

  14. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenating two binary files

    This is what it should be:
    Qt Code:
    1. // The Two Files are here.
    2. QString ticketName = ticketComboBox->currentText();
    3. QFile *ticket = new QFile(ticketName, NULL);
    4. ticket->open(QIODevice::ReadOnly);
    5. QFileInfo ti( ticketName );
    6.  
    7. QString fileName = directoryComboBox->currentText();
    8. QFile *file = new QFile(fileName, NULL);
    9. file->open(QIODevice::ReadOnly);
    10. QFileInfo fi( fileName );
    11.  
    12. // now put the two files together.
    13. QFile prnFile("out.prn");
    14. if (!outPrn.open(QIODevice::WriteOnly))
    15. return;
    16.  
    17. //first write the ticket
    18.  
    19. qint64 totalSize = ticket->size();
    20. qint64 chunkSize = 2048; //you can increase it and decrease it as you will
    21.  
    22. char *readBuffer = new char[chunkSize];
    23. while ( totalSize )
    24. {
    25. if( chunkSize > totalSize )
    26. chunkSize = totalSize;
    27. qint64 actuallyRead = ticket->read( readBuffer, chunkSize );
    28. if( actuallyRead > 0 )
    29. prnFile.write( readBuffer, actuallyRead );
    30. else
    31. break;
    32. totalSize -= actuallyRead;
    33. }
    34.  
    35. //Now Write the PDF file
    36. qint64 ptotalSize = file->size();
    37. qint64 pchunkSize = 2048; //you can increase it and decrease it as you will
    38.  
    39. char *preadBuffer = new char[pchunkSize];
    40. while ( ptotalSize )
    41. {
    42. if( pchunkSize > ptotalSize )
    43. pchunkSize = ptotalSize;
    44. qint64 pactuallyRead = file->read( preadBuffer, pchunkSize );
    45.  
    46. if( pactuallyRead > 0 )
    47. prnFile.write( preadBuffer, pactuallyRead );
    48. else
    49. break;
    50. ptotalSize -= pactuallyRead;
    51. }
    52.  
    53. // And when you're done:
    54. delete[] preadBuffer;
    55. prnFile.flush();
    56. delete ticket;
    57. delete file;
    To copy to clipboard, switch view to plain text mode 

  15. #14
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Concatenating two binary files

    Marcel,

    I got this to work, Ill compare my code to yours to see how far off I am.. Had all kinds of trouble with a SEG fault in the FTP function, seemed i had a qDebug that was tossing it all...

    Thanks for the help...

    this is actually getting easier... still painful, but easier...

Similar Threads

  1. Replies: 5
    Last Post: 22nd September 2006, 08:04
  2. UTF-16 files
    By jcr in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2006, 12:43
  3. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28
  4. Visual Studio 2003 and .vcproj files
    By mbjerkne in forum General Discussion
    Replies: 2
    Last Post: 1st February 2006, 00:51
  5. xml with binary question
    By TheKedge in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2006, 23:21

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.