Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. add line x in the QFile (text file )

add line x in the QFile (text file )

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zunneh
    wrote on last edited by
    #1

    Hi guys,
    my problem is , i want to write (add) a QString in a file ( text file ) in line x without losing the other lines
    for exemple my text file contains 200 lines , i want to add "hello world" in line 150 (so the file will contains 201 lines )
    I tried with file.write() but it always add in the last position of the text file

    Someone can help me ? thanks

    my english is average, please use simple words and try to be the most explicit, thank you

    JonBJ 1 Reply Last reply
    0
    • Z Zunneh

      Hi guys,
      my problem is , i want to write (add) a QString in a file ( text file ) in line x without losing the other lines
      for exemple my text file contains 200 lines , i want to add "hello world" in line 150 (so the file will contains 201 lines )
      I tried with file.write() but it always add in the last position of the text file

      Someone can help me ? thanks

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Zunneh
      This question gets asked many times :)

      You cannot do this just by e.g. write, that will either append or overwrite depending on how you opened the file.

      You wish to insert a line in the middle, moving everything else "downward". You can only do this one of two ways:

      1. Read whole file into memory array/list, insert line, write whole array/list back to file.

      2. Open existing file for read line at a time, open new, distinct file for output line at a time, loop reading/writing, insert extra line into output at appropriate point. On successful conclusion, delete original file, rename new file to old file. Personally I like this way (no memory usage, no potential "partial overwrite" of file), though #1 is less code to write.

      Z 1 Reply Last reply
      4
      • JonBJ JonB

        @Zunneh
        This question gets asked many times :)

        You cannot do this just by e.g. write, that will either append or overwrite depending on how you opened the file.

        You wish to insert a line in the middle, moving everything else "downward". You can only do this one of two ways:

        1. Read whole file into memory array/list, insert line, write whole array/list back to file.

        2. Open existing file for read line at a time, open new, distinct file for output line at a time, loop reading/writing, insert extra line into output at appropriate point. On successful conclusion, delete original file, rename new file to old file. Personally I like this way (no memory usage, no potential "partial overwrite" of file), though #1 is less code to write.

        Z Offline
        Z Offline
        Zunneh
        wrote on last edited by
        #3

        @JonB ok thank you

        my english is average, please use simple words and try to be the most explicit, thank you

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved