Thread: A simple file I/O problem

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    India
    Posts
    6

    A simple file I/O problem

    Hi all,
    I have a simple file I/O query. I have opened a new file for writing, I write it to some point then decide to go back to a particular point and write there again.
    What I am doing is that I am storing the position I want to write at later, in a variable using fseek function. But later when I try to write at that position the text there is overwritten rather than getting inserted. so the problem is how to insert the text using C.

    Thanks a lot

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by eecoder
    ...
    But later when I try to write at that position the text there is overwritten rather than getting inserted. so the problem is how to insert the text using C.
    The simple answer is that this is not how I/O works on files using the standard C file I/O functions. Writing always overwrites or extends the file, but never moves stuff around within the files.
    Insert obnoxious but pithy remark here

  3. #3
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >so the problem is how to insert the text using C.
    You need to rewrite the entire file, inserting the lines you want as necessary during the rewrite. Unless your system supports record oriented files, you can't insert and shift everything after forward, or delete and shift everything after back.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    India
    Posts
    6
    Does cpp support insertion and deletion of lines or text??

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    File manipulation is handled the same in both. The only reason it seems like you're editing the middle of a file, is because the whole thing is loaded into memory in your text editor of choice, and rewritten when you press the "save" button.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    India
    Posts
    6
    ok, and thank you all.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    India
    Posts
    6
    There is one way in which you can insert the text in between. You can initaly write some fake taxt at the place where you want to write, this mite not be usefull always, as u may not always be able to tolerate junk data in a file, but in some cases it may be usefull as you will not have to write the whole file again. This was the trick i was using, but wanted to know if there was any better way.
    Well thanks

  8. #8
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    using lseek() followed by write() will overwrite existing text, yes, but this is not insertion, it's replacement.
    Insert obnoxious but pithy remark here

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    India
    Posts
    6
    yes it is replacement, but you could have written some junk data at the place where you have to write later. If you know the length of string you have to write later.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Well to do this you would require to push all characters on and ahead of the insertion 1 char ahead. Leaving you with a blank space to type in. This could be done with some work.
    If its I/O to a file then it could be troublesome tho..
    Who needs a signature?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That post is about 4 years old Nathan - best to let the Zombie's sleep on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Binary file I/O
    By Tankists in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2009, 11:02 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  3. File I/O problem
    By dacbo in forum C Programming
    Replies: 4
    Last Post: 01-17-2006, 08:22 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. simple File I/O question
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 06:53 AM