Results 1 to 10 of 10

Thread: How to write data into a file

  1. #1
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How to write data into a file

    Hi All,

    I have a input data file which contains some text as the following, which is inputed using 4 lineedits n X Y Z then i need to write these into a text file when i press the enter button from the key board and the dialog closes then again i need to open the dialog to enter the next values and continues.


    #DATA 4 (n X Y Z)
    1 123.000 -123.000.123.000
    2 456.000 78.000 -12345.000
    .........
    .........
    .........

    and so on. But if i try to write the values into the file it is overwriting on the same place i.e., current values is writing into the file but the previous value is erased. i have attached my code and please help us.
    Attached Files Attached Files
    Last edited by grsandeep85; 20th July 2009 at 10:19. Reason: updated contents
    Thanks & Regards
    Sandeep G.R.

  2. #2
    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: How to write data into a file

    Use QIODevice::Append when opening the file. please only paste the relevant code directly in the post using the [ CODE ] tags.


    EDIT: And instead of creating a temporary QStringList, write the values direct in the stream.

  3. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write data into a file

    are you opening the file using QIODevice::Append ?

    EDIT : Too late....brother got too fast this time..
    Last edited by nish; 20th July 2009 at 10:23. Reason: somebody already answered

  4. #4
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to write data into a file

    Thanks Sir its working fine..

    And also i need to read the data which is written into the file by giving the serial number n(say 2) in lineedit n the corresponding values of X Y & Z( 456.000, 78.000, -12345.000 ) should be read and display the values in the X Y & Z lineedits. Please help us it would be a great pleasure.
    Thanks & Regards
    Sandeep G.R.

  5. #5
    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: How to write data into a file

    Either store the content of your file inside a QHash etc, and modify that and only when exiting the application save to file.

    Or perform full search on the file every time you a certain set of data.

    Or better use a database for that. SQLite is good for that since it is build in and a self-contained, serverless, zero-configuration, transactional SQL database engine.

  6. #6
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to write data into a file

    Hi Sir,

    I need to do it in lineedits only and not in SQLite, please help me how to proceed further.
    Thanks & Regards
    Sandeep G.R.

  7. #7
    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: How to write data into a file

    Quote Originally Posted by grsandeep85 View Post
    I need to do it in lineedits only and not in SQLite, please help me how to proceed further.
    The one has nothing to do with the other! (one is displaying and editing, the other storing)

    • You can store your data in a custom file, which you read/write each time you need it.
    • Store your data in a QHash and read/write the content to a custom file only on start and exit your application.
    • Like the above, only use XML with a QDomDocument
    • For storing the data use SQLite (which behaves like a custom file...)

  8. #8
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to write data into a file

    I am using qt-3.3.7 but QHash is not there in this version.
    Thanks & Regards
    Sandeep G.R.

  9. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write data into a file

    i think there is something in 3.3 like QDict

  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: How to write data into a file

    Quote Originally Posted by grsandeep85 View Post
    I am using qt-3.3.7 but QHash is not there in this version.
    If you can, update your version... Otherwise QHash was just an example, you also can use a simple QStringList or QMap, a own structure etc...

Similar Threads

  1. Replot large wav file data
    By Sachtech in forum Qwt
    Replies: 1
    Last Post: 6th January 2009, 09:12
  2. About File write operation
    By raghvendramisra in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2008, 11:41
  3. data is not appending to the file
    By sudheer in forum Qt Tools
    Replies: 2
    Last Post: 3rd April 2008, 12:39
  4. TCP Write Raw data
    By ^NyAw^ in forum General Programming
    Replies: 19
    Last Post: 23rd November 2007, 16:38
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15: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.