Thread: How to add a string from the top of the file

  1. #1
    Registered User
    Join Date
    May 2006

    How to add a string from the top of the file

    I need to know if a file has some data and I need to add a new value from the top. How can I do that?

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Toronto, Ontario, Canada
    I'm not quite sure what you necessarily want to do, but if you just wanted to check if a file has data in it, you could just check to see if its first character is EOF. If it is, it has nothing, if it isn't its got data.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Mar 2005
    It seems like a safe way to do it would be to open a new file.
    Write the new line to the new file.
    Write the contents of the current file to the new file.
    Then replace the current with the new.

    You could just read all of the current file into memory, and then rewrite the file. (the new line plus what you've stored), but there's more chance for something going wrong and destroying the file.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Check out remove() to delete the old file and rename() to rename the new file to the old file name.

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM