tags:

views:

124

answers:

4

I'm working on a program, that needs to load data from a text file upon starting and save data to THE SAME text file upon exit. I have the load working, and i have the save working, but for some reason I cant seem to have them both work within the same program.

This doesnt work...

ifstream loadfile("test.txt");
ofstream savefile("test.txt");

void load()
{
string name;

    while(!loadfile.eof())
    {
        getline(loadfile,name);
        cout<<"name " << name<<"\n";
    }
}

void save(User &name)
{   
savefile << name.getName() << endl;
}

Neither does this...

fstream file("test.txt");

void load()
{
string name;

    while(! file.eof())
    {
        getline(file,name);
        cout<<"name " << name<<"\n";
    }
}

void save(User &name)
{   
file << name.getName() << endl;
}

The thing is, I can save a list of names, which works fine... but as soon as i start the program, all the names from the list delete from the text file.

Also, I know that getline() gets the data from the text file as a string type, but how would i convert that to something like an int.

Thanks

+2  A: 
ofstream savefile("test.txt");

is equivalent to:

ofstream savefile;
savefile.open("test.txt", ios::out|ios::trunc);

That is, you're truncating the file as you open it. So, move the initialization of savefile to happen after you're done with your load call (I'd suggest doing it as late as possible, because if you crash after that initialization and before you're done saving, the save file is corrupted -- normally one writes to a different file and only does the rename at the very end when everything is safe on disk).

Alex Martelli
+3  A: 

Your files are being opened globally and never closed. Try:

void load()
{
    ifstream loadfile("test.txt");
    string name;
    while(!loadfile.eof())
    {
        getline(loadfile,name);
        cout<<"name " << name<<"\n";
    }
}
void save(User &name)
{
    ofstream savefile("test.txt");
    savefile << name.getName() << endl;
}
Greg Hewgill
I fail to see where you included 'closing' the files? loadfile.close() and savefile.close() are nowhere to be found??
Jesse O'Brien
The files will be closed when the stream objects go out of scope.
Paul Baker
This avoids clobbering the input file until after it has been read - which is important.
Jonathan Leffler
You may want to correct the while{} loop in load().
Martin York
+1  A: 

In your first sample, you may be running afoul of OS file locking, preventing you from opening the same file for both read and write. Remember to always check for failure when opening a file.

In the second sample, you don't rewind the file pointer. Use seekg to reset the stream pointer before trying to read. Keep in mind that although there's a seperate seekg and seekp, in practice they may refer to the same pointer, so it's always best to seek before switching between read and write.

bdonlan
+1  A: 
void load(){
    ifstream loadfile("test.txt");
    string name;

    while(!loadfile.eof())
    {
        getline(loadfile,name);
        cout<<"name " << name<<"\n";
    }
    loadfile.close(); // Call close() to free up resources again
}

void save(User &name)
{   
    ofstream savefile("test.txt");
    savefile << name.getName() << endl;
    savefile.close(); // Call close() to free up resources again
}

From Cplusplus I/O: "Once this member function is called, the stream object can be used to open another file, and the file is available again to be opened by other processes."

Jesse O'Brien
No need to call close(). When loadfile goes out of scope it is automatically closed. You should probably only be using close() if you specifically want to know about exceptions that are generated. Otherwise the destructor should do it.
Martin York