How do I open and write to a file w/out deleting previous contents?

I am staring to learn c++ as a hobby, and my current "project" is to document the workload of my
weightlifting sessions. As of now my code (when executed) will ask for various data (body-weight,
exercise type, weight, reps, etc.), it then will create a file and write the various combinations to
it that I want. My code looks like (ignoring the preamble -- #include <iostream>, etc. -- and
cutting the bulk to save space):

1
2
3
4
5
6
7
8
9
10
11
int main() {

        ofstream outputFile;
        outputFile.open("programming.txt");

        /* Rest of the code goes here */

        outputFile.close();

   return 0;
}


When I compile and run my program it works great (for now), but it will create a new
"programming.txt" each time (as you'd expect). What I would like is to append to the current txt
file each time. For example, on day 1 I run my program and create the txt file with the data, then
on day 2 I run my program and it adds the new workout session data to the txt file below (or above)
the data for day 1. And so on.

Any help or pointers to relevant references would be great, and thanks in advance.
Last edited on
You really should start getting used to using the constructor to open the file, and letting the destructor close the file. As you seem to know the default open mode for an ofstream truncates the file, if you want to retain the file contents you need to use the ios::app open mode:

1
2
3
4
5
6
7
8
9
10
11
#include <fstream>

int main() {

    std::ofstream outputFile("programming.txt", std::ios::app);

    /* Rest of the code goes here */


    return 0;
}
jlb,
Thanks for the response. I have a quick question(s), if you have time. What exactly do you mean by "constructor"/"destructor". I'm sure my limited understanding is causing me problems, but what I have in mind is: by defining the outputFile we're allocating memory for the file, then as I write to it data is being added to this memory location and then when I kill the program the memory is wiped (but the file still remains). Is this what you mean? Sorry for so many questions.
jlb,
Also, the ios::app mode is exactly what I wanted! Thank you. I've read a little about it here http://www.cplusplus.com/reference/ios/ios_base/openmode/ but I don't see how to append above the previous entry. There more I think about this the more I feel like this would be hard, because I would want to write for each training session in chronological order (first down to last), but then have this entire block of output to appear above (reverse chronological order) the previous training session. For example, what I'd like is:

Thurs 05/12/16
1. exercise: 50kg @ 4 reps.
2. exercise: 60kg @ 1 rep.
3. exercise: 80kg @ 6 reps.
.
.
.
-------------------------------------------------

Wed 05/11/16
1. exercise: 30kg @ 2 reps.
2. exercise: 40kg @ 2 reps.
3. exercise: 50kg @ 8 reps.
.
.
.
------------------------------------------------
and so on.

Is there a prepend state for ios?
What exactly do you mean by "constructor"/"destructor".

ofstream is a class that has quite a few different member functions. Of these member functions there are several constructors, member functions called when you create an instance of the class, and one destructor that closes the open file, then frees any memory associated with the class. http://www.cplusplus.com/reference/fstream/ofstream/


but I don't see how to append above the previous entry.

Basically you can't append data in the middle of the file, you can only replace items, not add content. To do what you want you would need to first read the file, sort the data into the correct order, then erase the file contents and write the information to the file in the correct order.
Thank you again. I will start reading the link.
Reading has got me thinking, and now I have some new questions, related to what is happening on a deeper level. In my mind my program does the following: I run my program and it assigns/allocates spots in my memory where the relevant data is stored, called upon, computed with, etc., then it will print off some of the data being stored, and then finally the program gets terminated, at which point those spots in the memory sea are filled back in and all knowledge of the program running is gone (other than what has been printed out).

I know this is not what is happening, but hey I'm still learning :), and rather than focusing on everything I'd like to simply focus on the last part: the part about the data being destroyed after the program terminates. Is this really true? If so, how could I keep specific data safe even during this termination procedure?

For example, suppose you wanted to add your favorite number for today to your favorite number from yesterday, and keep running this addition loop indefinitely (10 + 12 = 22 + 7 = 29 + 5 = 34 + ....) daily, but you don't want to keep the program running all day long. You want to be able to terminate the program once you've entered you daily favorite number (or even shut-down your pc) without losing the previous sums. This is important for my project because totaling the weight and reps over a macrocycle (think month) is important when analyzing programming.

Once again, thanks for the explanations and links...this is really cool stuff!
You want to be able to terminate the program once you've entered you daily favorite number (or even shut-down your pc) without losing the previous sums.

This the purpose of files. You write the information to files to save the data for later use.



Is it possible to bypass this...to write the data to memory permantly (or at least until specifically telling the computer to delete it)? I know this could get very dicey, especially for someone like me who really has no idea how a computer works, and see how writing to a file would be much safer (once again for me), but I'm curious if this possible, if it's done in practice, and if so, what should I look into to read more about it?
Is it possible to bypass this...to write the data to memory permantly (or at least until specifically telling the computer to delete it)?


I don't understand why you think you want to do this. You can simply write whatever data you want (favorite numbers and so on) to a file, and then read whatever data you want from the same file whenever you please, whether it's now, tomorrow, or fifty years from now.

I run my program and it assigns/allocates spots in my memory where the relevant data is stored, called upon, computed with, etc., then it will print off some of the data being stored, and then finally the program gets terminated, at which point those spots in the memory sea are filled back in and all knowledge of the program running is gone


I think you're confused. Just because your program's resources are released when it terminates, doesn't mean that any files it writes during it's execution are deleted or have their contents removed. You don't loose all the data on your hard drive when you turn off your PC just because the operating system terminates.
Last edited on
Yes, I can see how it's better (especially for me) to write to a file and read from that...but I am curious if it's done other ways. Also, when I define a data type and assign it a value, that value is destroyed when I terminate my program. This is what I meant when I said the data "spots" in the memory are wiped. Is this not the case? If those values are still there then one should easily be able to retrieve them after the program has run and been terminated, no?
Last edited on
Topic archived. No new replies allowed.