Results 1 to 14 of 14

Thread: Concatenating two binary files

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

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

    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 21:45.

  3. #3
    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?

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

    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

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

    nbkhwjm (20th April 2007)

  6. #5
    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!!

  7. #6
    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!!

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

    Default Re: Concatenating two binary files

    You're welcome

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

    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 

  10. #9
    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, 09:04
  2. UTF-16 files
    By jcr in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2006, 13: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, 11:28
  4. Visual Studio 2003 and .vcproj files
    By mbjerkne in forum General Discussion
    Replies: 2
    Last Post: 1st February 2006, 01:51
  5. xml with binary question
    By TheKedge in forum Qt Programming
    Replies: 7
    Last Post: 13th January 2006, 00: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
  •  
Qt is a trademark of The Qt Company.