CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 1999
    Location
    North Sydney, NS
    Posts
    445

    inserting data in a file

    Is it possible to use fstream to insert data in the middle of file? Do I need to rewrite the file each time? What if I want to remove data from the middle of a file?

    I have a bunch of lists I'd like to save to the HD but the only thing that works is rewriting the whole list. Does this become a problem as the lists get larger?

    Paul
    I know how to build. What to build is a completely different story.

  2. #2
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: inserting data in a file

    You can position the read and write cursor for your output stream anywhere you want I believe.

    Go to this website and have a look at seekg and seekp.

    I don't have much experience with these and haven't used them in quite a few years, so I am not entirely sure they will do what you want.

    You can also checkout this website which gives quite a bit of info on C++ I/O and streams.

    Not sure if this helps, but you can open an existing file that is already on the hard disk for output in append mode. Meaning anything dumped to it will be tacked on the end of it, and won't overwrite anything existing.
    Code:
    ofstream myFile("myFile.txt", ios::app );
    Here is a link to all the fstream modes.

    Hope this helps
    Last edited by dcjr84; October 18th, 2006 at 12:37 AM.
    Please rate my post if you felt it was helpful

  3. #3
    Join Date
    Dec 1999
    Location
    North Sydney, NS
    Posts
    445

    Re: inserting data in a file

    It seems seekp will position anywhere in the file for writing but overwrites the data already at that position. At least that's the behavior I'm getting right now.
    I know how to build. What to build is a completely different story.

  4. #4
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: inserting data in a file

    Did you try opening the file in append mode, and then using seekp?

    Perhaps that will not overwrite your data.

    I have always found in my experience that writing data to anywhere other than the beginning or end of a file is a real pain.
    Please rate my post if you felt it was helpful

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