CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 1999
    Posts
    102

    inserting text to file

    Hi All,

    Sorry if this is already answered - but my searches on codeguru are timing out if it has more then one word in the search -today

    I am trying to insert text to an open input text file.
    I have no problem writing to the file - but I want to INSERT text somewhere in the middle - not just append to the end.
    Is there a function that does that - or do i have to manully take care of it i.e :
    1. get to the insersion position
    2. cpy the rest of the file to a buffer
    3. write the text i want to add to the file
    4. append the "rest" buffer

    I would preffer to use a "ready made" function if there is one.

    Thanks in advance

    Dani.

  2. #2
    Join Date
    Feb 2003
    Location
    USA
    Posts
    26
    Use fseek(filePointer, numberOfBytesFromOrigin, origin)

    where origin having value of SEEK_CUR or SEEK_END or SEEK_SET

    Refer documentation for full details.

  3. #3
    Join Date
    Sep 1999
    Posts
    102
    gangaprasad,

    I am using seekg and seekp - to find my way in the file - this is not my problem.
    May be i was not clear -
    My problem is to insert - in oposite of override - text in a position in the text which is not the end of file - which means - to add string that will move the rest of the text "foeward" in the file - and not add the text "on" the exsiting one...

    Seek will only help me move the pointer in the file - but not deal with the "overriden" text...

    All the best

    Dani

  4. #4
    Join Date
    Jun 2002
    Posts
    1,417
    it is not possible to just insert the text. You must rewrite at least part of the file. two ways I can think of:

    1. rewrite the entire file. This could be time consuming if its a big file.

    2. Read the file into memory starting from where you want to insert the new string. Write the new string to the file, then rewrite the rest of the file.

  5. #5
    Join Date
    Sep 1999
    Posts
    102
    stober,

    Yes, this is what i did.
    I was hoping there was a ready made function for that...

    Thanks.

  6. #6
    Join Date
    Jun 2002
    Posts
    1,417
    Nope. I don't know of any other way to do it.

  7. #7
    Join Date
    Feb 2003
    Posts
    53
    Naah!
    There has to be a way to insert text into a file.

  8. #8
    Join Date
    Jun 2002
    Posts
    1,417
    well, if you discover it I'm sure you could make a ton of money on the copyright.

  9. #9

    Best way.....

    Is to load the file into memory and insert the text that way. Most people would just use std::string - easiest way, then write back to the file.

    -OR-

    You could open a temporary empty file, read up to the part you want to insert in, write that in the temp, write your inserted text, read the rest of the source text file - write that in the temp, close the source and temp, delete the source, and move/rename the temporary to the original source filename.

    Once again - elementary.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured