adding text to top

This is a discussion on adding text to top within the C++ Programming forums, part of the General Programming Boards category; Hey, I am trying to add text to the top of a text file via a c++ program. Sound confusing? ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    adding text to top

    Hey, I am trying to add text to the top of a text file via a c++ program. Sound confusing? Okay, my program starts out by asking for input. When done entering the input, it outputs it to a text file. Now, I want to be able to open the text file later on and add some more info. However, I want the new data to be added to the top of the text file already created (not deleting previous data)
    Example of text file layout:
    Code:
    new data
    old data
    old data
    if you could point out a good resource for this, that would be most helpful.
    thanks,
    Ben

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) Read in all the lines from the file (e.g. using an array of strings)
    2) Close the file and reopen it, which will erase the file.
    3) Read in the input from the user.
    4) Write the input to the file
    5) Write the array of strings to the file.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    hmm. Well, I was hoping I wouldn't have to do that. I was hoping that there would be a simple solution. Like ios::nocreate and some other instruction to keep it at the top. Oh well. Thanks for the help.

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    yes you can open the file with ios::app
    and then move the postion to the beginning fo the file,
    with seekg()

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I was hoping that there would be a simple solution.
    That is a simple solution, and it's easy to code.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Ilovevectors: thanks for the tip.

    7stud: It sounds easy enough to code. I just wanted it to be easier in that I didn't want to have to create somthing else for this to work (reading data into array). Thanks for the suggestion though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  2. OpenGL, loading BMP Textures?
    By Zeusbwr in forum Game Programming
    Replies: 12
    Last Post: 12-09-2004, 05:16 PM
  3. Odd 3D Invis Objects?
    By Zeusbwr in forum Game Programming
    Replies: 4
    Last Post: 12-07-2004, 07:01 PM
  4. Adding to text in an edit
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 02-29-2004, 03:21 PM
  5. Small HTML question
    By Thantos in forum Tech Board
    Replies: 4
    Last Post: 12-29-2003, 12:37 AM

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