#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    1
    Rep Power
    0

    Question 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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.
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "i interpreted it as wanting"

    I make of the post much couldn't sense .
  8. #5
  9. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    ^^LOL sense not much made. thinks him must be we psychic.

IMN logo majestic logo threadwatch logo seochat tools logo