April 18th, 2003, 11:16 AM
c++ file i/o question
If I have a method function that saves all my private data to a file in a class I created. And in main I have a loop that executes that “save method function” for as many objects that I happen to create, the first call to “save method” in the loop executes correctly and saves data to the file, but the next time the loop calls “save method” how do I (in the save method function) save the data after the previous data already saved in the file? Is there a function to test what is in a file or how many characters are in a file? Any feedback would be great.
April 18th, 2003, 04:42 PM
It sounds like you may be reopening the file everytime and overwriting what's in there. I would suggest you only open the file once, and then writing to it will continue where you left off. Or, if you have to keep opening the file everytime, make sure you open it to append to the file:
const char* filename = "C:\\TestData\\input.txt";
ofstream outFile(filename, ios::out | ios::app);
When you do this:
the default mode is:
ios::out | ios::trunc
which opens the file for output and overwrites anything in the file.
Last edited by 7stud; April 18th, 2003 at 04:49 PM.
April 18th, 2003, 04:58 PM
i interpreted it as wanting to first check if there is data in the file, and if there is then append the newest data to the end of the previous segment. in your saveData() method, u will want to have some sort of check to see if there is data in the file, and where this data ends. is your file sequential or random?
edit: just to add a little to 7stud's, if in fact you are wanting to append data, all u have to do is use: ios::ate and it will automatically move to the end of the file.
Last edited by infamous41md; April 18th, 2003 at 05:01 PM.
April 19th, 2003, 04:55 AM
"i interpreted it as wanting"
I make of the post much couldn't sense .
April 19th, 2003, 11:05 AM
^^LOL sense not much made. thinks him must be we psychic.