Results 1 to 13 of 13

Thread: Problems reading content of QFile

  1. #1
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Problems reading content of QFile

    I need to send a sqlite file up to a website using post. So what I do is that I open the file and try to read the content of it into a QString that I with help of QNetworkRequest can send to the server. I can handle the request as the file is sent to the server but the file is just empty. The QString str here is always empty. Am I reading the content of the sqlite file wrong? Can you anything wrong with my code?

    This is the code for reading the file:
    Qt Code:
    1. QFile file("database.sql"); //I have tested that this excists
    2. file.open(QIODevice::ReadOnly);
    3. QDataStream in(&file);
    4. QString str;
    5. in.setVersion(QDataStream::Qt_4_6);
    6. in >> str; //str is always empty here
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading content of QFile

    test file.open() for success.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: Problems reading content of QFile

    Thanks but QFile.open() seems to want a parameter like QIODevice::ReadOnly. just open() isnt working for me. Was that what you meant? What I need to do is get the content of the file into a variable so that I can send it to server. Am I doing something wrong in my code?

  4. #4
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading content of QFile

    Please excuse my ignorance but I am not familiar with the intenals of a sqlite file. Is it binary?

    In which case you are trying to feed binary data into a string.

  5. #5
    Join Date
    Mar 2010
    Posts
    11
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems reading content of QFile

    @martinn

    Check for success like this.
    if( file.open(QIODevice::ReadOnly) )
    {
    //your code
    }

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems reading content of QFile

    Check if your path is right and you might better use QTextStream.

  7. #7
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: Problems reading content of QFile

    I updated my code with check that the file excist and is open. It is open but I still can't get the str to get all the content of the sqlite file.

    Qt Code:
    1. QFile file("database.sql")
    2. if (file.open(QIODevice::ReadOnly)) {
    3. QDataStream in(&file);
    4. QString str;
    5. in.setVersion(QDataStream::Qt_4_6);
    6. in >> str; //str is always empty here
    7. }
    To copy to clipboard, switch view to plain text mode 

    When testing with QTextStream instead it just set the str variable to "SQLite". What I need is the content of the file, how can I do that?

  8. #8
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading content of QFile

    Try opening the file in a text editor - Vi, Emacs, Notepad or what ever.

    If you see gibberish then this is a binary file and can not be held in a string.

    You will need to dump the sqlite file as text first and open that.

  9. #9
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: Problems reading content of QFile

    Yes it looks gibberish but how can I then send it to my server? I would have the same problem if I wanted to send an image or another binary file.

    This is my code, should I do something different?
    Qt Code:
    1. QFile file("database.sql");
    2. if (!file.open(QIODevice::ReadWrite)) {
    3. return;
    4. }
    5.  
    6. QDataStream in(&file);
    7. in.setVersion(QDataStream::Qt_4_6);
    8. in >> data ;
    9.  
    10. QString boundary;
    11. QByteArray dataToSend; // byte array to be sent in POST
    12.  
    13. boundary="-----------------------------7d935033608e2";
    14.  
    15. QString body = "\r\n--" + boundary + "\r\n";
    16. body += "Content-Disposition: form-data; name=\"database\"; filename=\"database.sql\"\r\n";
    17. body += "Content-Type: application/octet-stream\r\n\r\n";
    18. body += data;
    19. body += "\r\n--" + boundary + "--\r\n";
    20. dataToSend = body.toAscii();
    21.  
    22. QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this);
    23. QNetworkRequest request(QUrl("http://www.mydomain.com/upload.aspx"));
    24. request.setRawHeader("Content-Type","multipart/form-data; boundary=-----------------------------7d935033608e2");
    25. request.setHeader(QNetworkRequest::ContentLengthHeader,dataToSend.size());
    26. connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(sendReportToServerReply(QNetworkReply*)));
    27. QNetworkReply *reply = networkAccessManager->post(request,dataToSend); // perform POST request
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems reading content of QFile

    Quote Originally Posted by martinn View Post
    Yes it looks gibberish
    ...then you should not use the sql file ending, since that indicates that it is a plain text file! Then forget about QTextStream and deal with QDataStream or better don't use any stream at all.

  11. #11
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading content of QFile

    To upload the binary file, just open it like you have in lines 2-3 and use 'file' in place of 'dataToSend'.

    As Lykurg has pointed out, there is no need to use streams or convert the file to ascii.

    If you are trying to upload text from a sqlite DB to a web page for people to read, you may need to run a select query to get the text out of the DB and then upload that.

  12. #12
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: Problems reading content of QFile

    Thanks! And I'm so sorry but I must be slow or something, I really don't understand where to place the file. :-)

    Can you show me in the code I posted how I can send the file?

  13. #13
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: Problems reading content of QFile

    Anyone know how to solve this? Sorry but I really can't get it working..

Similar Threads

  1. Problems reading XML with QDomDocument
    By grantbj74 in forum Qt Programming
    Replies: 9
    Last Post: 27th August 2009, 08:14
  2. QFile, QDataStream reading binary data
    By yren in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2009, 06:34
  3. QFile and QTextStream problems
    By uchennaanyanwu in forum Newbie
    Replies: 0
    Last Post: 7th August 2008, 02:44
  4. Reading File using QFile and QTextStream
    By atm in forum Qt Programming
    Replies: 4
    Last Post: 13th July 2006, 23:12
  5. QT4 - get content of a QAbstractItemModel
    By incapacitant in forum Newbie
    Replies: 4
    Last Post: 18th March 2006, 06:23

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.