|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.
|
|
#2
|
|||
|
|||
|
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: ofstream outFile(filename); 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. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
"i interpreted it as wanting"
I make of the post much couldn't sense . |
|
#5
|
||||
|
||||
|
^^LOL sense not much made. thinks him must be we psychic.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > c++ file i/o question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|