开发者

Problem reading and writing from same file within same program... C++

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 &nam开发者_开发技巧e)
{   
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


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;
}


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).


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.


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."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜