inserting text at the beginning of a file

This is a discussion on inserting text at the beginning of a file within the C++ Programming forums, part of the General Programming Boards category; Hi everybody! I need to write at the beginning of a file that already has a content and I want ...

  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,006
    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
    32,851
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,048
    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
    22,303
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,262
    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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,262
    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, 05: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, 09:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21