Results 1 to 5 of 5

Thread: Saving and reading text to and from Base64

  1. #1
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Saving and reading text to and from Base64

    I need to save some information (text) into a file. However, I need to obfuscate the contents to prevent tampering, or at least make it harder. The easiest solution that came to mind was to convert the QString data to CString, stream it to QByteArray, convert it to Base64 using QByteArray::toBase64(), then save it to a file using QDataStream. I don't have problem with saving.

    Reading the information from the file, however, hasn't been successful. Upon reading the information, only a single word is retrieved, not the whole text. I need some advice on this one. Here's a simplified version of my code:

    Qt Code:
    1. QString strTest = "This is a string.$";
    2. QString strTest2 = "Another string.$";
    3.  
    4. QFile testFile( "./Test.ini" );
    5. if( testFile.exists() ) {
    6. testFile.open( QIODevice::Append );
    7. QByteArray byteStrTest2( strTest2.toStdString().c_str() );
    8. qDebug() << "strTest2.toStdString().c_str() : "
    9. << strTest2.toStdString().c_str();
    10.  
    11. QDataStream writer( &testFile );
    12. writer << byteStrTest2.toBase64();
    13. testFile.close();
    14. }
    15. if( !testFile.exists() ) {
    16. testFile.open( QIODevice::WriteOnly );
    17. QByteArray byteStrTest( strTest.toStdString().c_str() );
    18. qDebug() << "strTest.toStdString().c_str() : "
    19. << strTest.toStdString().c_str();
    20.  
    21. QDataStream writer( &testFile );
    22. writer << byteStrTest.toBase64();
    23. testFile.close();
    24. }
    25.  
    26. readFromByte( &testFile );
    27. }
    28.  
    29. void readFromByte(QFile *file) {
    30. file->open( QIODevice::ReadOnly );
    31. QDataStream reader( file );
    32. QByteArray byteFileContents;
    33. reader >> byteFileContents;
    34.  
    35. qDebug() << "byteFileContents : " << byteFileContents;
    36. qDebug() << "byteFileContents.fromBase64( byteFileContents ) : "
    37. << byteFileContents.fromBase64( byteFileContents );
    38.  
    39. byteFileContents = byteFileContents.fromBase64( byteFileContents );
    40. qDebug() << "byteFileContents.size() : " << byteFileContents.size();
    41.  
    42. char *chrFileContents = new char[ byteFileContents.size() + 1 ];
    43. strcpy( chrFileContents, byteFileContents.data() );
    44. QString strFileContents = QString::fromAscii( chrFileContents, byteFileContents.size() );
    45. QStringList strings = strFileContents.split( '$', QString::KeepEmptyParts, Qt::CaseSensitive );
    46.  
    47. QString strDisplay;
    48. foreach(strDisplay, strings) {
    49. qDebug() << "Value : " << strDisplay; // Output is always "Value : This is a string."
    50. }
    51. file->close();
    52.  
    53. qDebug() << "sizeof( chrFileContents ) : " << sizeof( chrFileContents );
    54. qDebug() << "sizeof( char ) : " << sizeof( char );
    55. }
    To copy to clipboard, switch view to plain text mode 
    Thanks in advance.
    Last edited by Chase; 18th April 2011 at 12:14.

  2. #2
    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: Saving and reading text to and from Base64

    I think you have over-complicated the exercise. Something like:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QString strToSave("Some test data");
    9. QFile output("output.txt");
    10. if (output.open(QIODevice::WriteOnly)) {
    11. output.write(strToSave.toUtf8().toBase64());
    12. output.close();
    13. }
    14.  
    15. QFile input("output.txt");
    16. if (input.open(QIODevice::ReadOnly)) {
    17. QByteArray asSaved = input.readAll();
    18. QString strRestored(QByteArray::fromBase64(asSaved));
    19.  
    20. qDebug() << "Saved:" << strToSave;
    21. qDebug() << "As:" << asSaved;
    22. qDebug() << "Restored:" << strRestored;
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving and reading text to and from Base64

    That's what my first code looked like, with the only difference being the QIODevice flag -- I used QIODevice::Append if the file already exists. It corrupts the file after several writes.

    Thanks anyway.

  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: Saving and reading text to and from Base64

    Quote Originally Posted by Chase View Post
    It corrupts the file after several writes.
    What does this mean?

    If you concatenate a series of QDataStreams, each containing a QByteArray, then what your file contains is a series of QDataStreams not one combined QDataStream. Your read code is expecting to read the entire file but it getting the first QByteArray in the file.

    Is there a reason you are using QDataStream for text-only data?

    A revised version
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QString strToSave("Some test data");
    9. QFile output("output.txt");
    10. if (output.open(QIODevice::Append)) {
    11. QDataStream out(&output);
    12. out << strToSave.toUtf8().toBase64();
    13. output.close();
    14. }
    15.  
    16. QFile input("output.txt");
    17. if (input.open(QIODevice::ReadOnly)) {
    18. QDataStream in(&input);
    19. QByteArray asSaved;
    20.  
    21. while (!input.atEnd()) {
    22. in >> asSaved;
    23.  
    24. QString strRestored(QByteArray::fromBase64(asSaved));
    25.  
    26. qDebug() << "Saved:" << strToSave;
    27. qDebug() << "As:" << asSaved;
    28. qDebug() << "Restored:" << strRestored;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 19th April 2011 at 08:30.

  5. #5
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving and reading text to and from Base64

    If I use QFile::readAll() to read strings from the file, I get lots of garbage in the final string after performing write operations on it several times. In other words, the approach that you pointed out only gives expected result on first and second write and read operations. After that, the file will be filled with garbage.

    This is the output that I'm getting using your code, which is very similar to my first implementation:

    "This is a string.$Another string.$♦▬µ≈F?W"7G&?µrΆœ@Another string.$♦▬µ≈F?W"7G&?µrΆœ@"
    That's why I decided to try QDataStream to do the job instead.

    Edit: This post was made before I saw your edited post. "QFile:pen()" should be "QFile::readAll()".
    Last edited by Chase; 19th April 2011 at 09:21.

Similar Threads

  1. Reading from text file
    By jerkymotion in forum Qt Programming
    Replies: 5
    Last Post: 17th March 2011, 11:26
  2. Reading text from a PlainTextEdit
    By Bertie in forum Newbie
    Replies: 5
    Last Post: 9th March 2011, 14:24
  3. Saving Text and Images as a file
    By rleojoseph in forum Qt Programming
    Replies: 2
    Last Post: 15th February 2011, 09:15
  4. Saving rich text to XML file
    By been_1990 in forum Newbie
    Replies: 5
    Last Post: 2nd August 2010, 23:46
  5. Problems saving a text file
    By aarelovich in forum Qt Programming
    Replies: 3
    Last Post: 27th September 2009, 14:39

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.