How do you write to a file one line at a time?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dbarten1982
    New Member
    • Feb 2010
    • 8

    How do you write to a file one line at a time?

    I am in the planning stages of a project I am going to be building and for one part I am going to need it to write a string to a file one line at a time. The overall goal is for it to write one line each time the program is run.(Similar to storing "High Scores" in a game.) I am still learning C++ and my text book only explains writing data to a file as a whole not adding data on to existing data in a file. I am using <fstream> and so far on other projects the data just gets overwritten, how do I get it to write the new data to a new line? I haven't written the code yet so I have none to display, but is there a function to use to do this?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Look at the fstream reference.

    You are probably going to need to use a slightly different set flags for the open mode. I suspect you will need to include ios_base::ate to position the file pointer at the end of the file on opening.

    Alternitively open the file can call fstream::seekp to position the output file pointer at the end of the stream so it doesn't overwrite the data already in the file.

    Comment

    Working...