Results 1 to 8 of 8

Thread: Insert data in a text file

  1. #1
    Join Date
    Jul 2011
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Insert data in a text file

    Hello everybody,

    I am facing an issue:
    I want to be able to insert some text at the end of every line of a text file.
    I tried many things with QTextStream on a QFile but i am still not able to do the trick.

    Initialization of a test file.
    I store in the endLinePos the position of the endline character.
    Qt Code:
    1. in_file->open(QFile::ReadWrite | QFile::Truncate);
    2. qDebug() << "INITFILE";
    3. QTextStream in(in_file);
    4. for(int i=0;i<10;i++)
    5. {
    6. qDebug() << "INITFILE - line - "<<i;
    7. in << "123456789\n";
    8. endLinePos[i]=(i*10+9);
    9. }
    10. qDebug() << "END INITFILE";
    11. in_file->close();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. in_file->open(QFile::ReadWrite);
    2. QTextStream in(in_file);
    3. for(int lineNum=0; lineNum<10; lineNum++)
    4. {
    5. in.seek(endLinePos[lineNum]);
    6. in<<"toto";
    7. }
    To copy to clipboard, switch view to plain text mode 

    The output:
    Qt Code:
    1. 123456789toto456789toto456789toto456789toto456789toto456789toto456789toto456789toto456789toto456789toto
    To copy to clipboard, switch view to plain text mode 

    I can see that my data "toto" has been placed at the right position but it replaced the original content.
    My aim would be to generate an output like
    Qt Code:
    1. 123456789toto
    2. 123456789toto
    3. 123456789toto
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Loading the full file in the memory is not a solution: The real data are far too big (each line : 130000 char in a file having around 500 lines).


    Thanks for your advices!
    Korwin
    Last edited by korwin; 13th July 2011 at 15:07. Reason: spelling corrections

  2. #2
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Insert data in a text file

    .............
    Last edited by meazza; 13th July 2011 at 15:18. Reason: Missunderstod

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Insert data in a text file

    You can use a new file QTemporaryFile and after completed you can copy in the old file
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Jul 2011
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Insert data in a text file

    Hello meazza,
    Unfortunatly it doesn't work.
    Here is the output

    Qt Code:
    1. 123456789
    2. 123456789
    3. 123456789
    4. 123456789
    5. 123456789
    6. 123456789
    7. 123456789
    8. 123456789
    9. 123456789
    10. 123456789
    11. totototototototototototototototototototo
    To copy to clipboard, switch view to plain text mode 

    If i look at the definition QIODevice::Append : The device is opened in append mode, so that all data is written to the end of the file.
    It looks like normal.

    Korwin

  5. #5
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Insert data in a text file

    Ye realized that it wouldnt work, misunderstod what you wrote. Thats why I removed the post

  6. #6
    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: Insert data in a text file

    you can not insert data in between of a text file. move to sqlite for storing the data.

  7. #7
    Join Date
    Jul 2011
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Insert data in a text file

    You are totally right and this csv export is just a temporary feature. We'll move to sql database soon.
    As it is quite urgent i wont spend too much effort on this problem (already took me long time) and i will use a temporary file as suggested before.

    Korwin

  8. #8
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Insert data in a text file

    You can't "insert" data into a text file. You can either overwrite or append. To acheive what you want you must read the file and write a new file: Read a line, write it to the new file (but without writing the newline character), then write the new data you want and a newline. Repeat.

Similar Threads

  1. Replies: 8
    Last Post: 8th May 2012, 09:39
  2. Problem: Reading and editing text file data
    By dipeshtech in forum Newbie
    Replies: 2
    Last Post: 2nd May 2011, 23:47
  3. How to query data by id in a text file
    By SamSong in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2009, 10:58
  4. Insert data to QTableView
    By Lodhart in forum Qt Programming
    Replies: 1
    Last Post: 23rd April 2009, 09:38
  5. Replies: 2
    Last Post: 17th November 2006, 11:25

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.