Thread: inserting text at the beginning of a file

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    15

    inserting text at the beginning of a file

    Hi everybody! I need to write at the beginning of a file that already has a content and I want to insert in it without overwriting what it already contains. Do you know any function to do this? I'm working with streams, by the way, so I need a function that works with ofstreams. Thanks a lot!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Make a new file. Add the line at the beginning. Read the old file and write its contents to the new file. Close the new file. Rename the new file with the old file's name.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There is no insert text function, you have to do it yourself

    Open a new file
    Write the data you want at the start of the file
    Copy the contents of the old file to the new file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    15
    I've already thought about it but unfortunately I'm not allowed to use any other file (it's one of the restrictions of the exercise). Anyway, I think I can do it another way. Thanks!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you can only use one file, you could read all the data in, truncuate the file, and then write all the data (including the new line at the beginning) back to the file. You shouldn't do this in practise; rather, you should do as Salem suggested, otherwise you program might be interrupted while writing the data out, causing data loss. But if your requirement is one file, then do so.

    IMHO that's a silly restriction.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    15
    Thanks! Although I've thought of another way to solve the exercise without doing the file thingy. Thanks a lot anyway!

    IMHO that's a silly restriction
    I totally agree.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Although I've thought of another way to solve the exercise without doing the file thingy.
    What would that be? I dont see how you can work with files without working with files
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Couldn't he read the whole file into memory, then add what he wants to read to the string, or whatever, then write it all back, overwriting what was there originally?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Couldn't he read the whole file into memory, then add what he wants to read to the string, or whatever, then write it all back, overwriting what was there originally?
    That was what pretty much what dwks suggested.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by laserlight
    That was what pretty much what dwks suggested.
    Good point ... wasn't paying attention.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    15
    Quote Originally Posted by laserlight
    What would that be? I dont see how you can work with files without working with files
    It requires a really long explanation... It's just that I can avoid using the file in question if I approach the exercise in a really different way. It doesn't have anything to do with my original question, so you'd have to know what the entire exercise is about to get it, if you know what I mean... Sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM