Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to create a file and save program progress in file.
As my object is Complex and Dynamic in size thus i can't use binary mode.
Thus i tried to save data in normal text file, but can't figure out how to open and edit the file and save it again. i tried following code but keep getting error. Can any one help me?

C++
#include <fstream>
#include <limits>
#include<iostream>
using namespace std;

std::fstream& GotoLine(std::fstream& file, unsigned int num){
    file.seekg(std::ios::beg);
    for(unsigned int i=0; i < num - 1; ++i){
        file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    }
    return file;
}

std::fstream& GotoLineWrite(std::fstream& file, unsigned int num){
    file.seekg(std::ios::beg);
    for(unsigned int i=0; i < num - 1; ++i){
        file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    }
    long len=0;
    len=file.tellg();
    file.seekp(len,std::ios::beg);
    return file;
}


int main(){
    using namespace std;
    fstream file("bla.txt",ios::in|ios::out);
    GotoLine(file, 4);
    int line=0;
    file >> line;
    cout<<line<<endl;
    line=line+14;
    GotoLineWrite(file,5);
    file<<line;
    cout<<line;
    return 0;
}
Posted

Text files are easy to create, but hard to modify.
Consider this:
1. Load the existent file into an object.
2. Modify the object.
3. Serialize (save) the entire object to a new, temporary file.
4. Copy the new file to the old one (overwriting it).

For reasonably small files, this should be both simple and efficient.
If your file is very big (hundreds of megabytes or more), consider using some kind of database.

Hope this helps,

Pablo.
 
Share this answer
 
Comments
Sizil Krishna 4-Nov-12 9:59am    
Thanks Pablo, i guess i will go with DB.
As my object is Complex and Dynamic in size thus i can't use binary mode.

That is a very good reason why you should use binary mode. You can write an object direct to your file and read it back direct into an object, which makes this sort of process very simple to implement.
 
Share this answer
 
Comments
Sizil Krishna 4-Nov-12 9:59am    
Thanks Richard actually i can't fix size of my object this is the reason i am ignoring writing binary object. As i found that writing binary objects needs object to be of fixed size, but i am using strings and vectors whose limit is dependent on user and is very high. If anything is there i am missing or am wrong, kindly guide me.

Thank you.
Richard MacCutchan 4-Nov-12 10:03am    
Writing binary objects does not require them to be of fixed size. All you need to do is add information to the file which specifies the type of object (or field) and, if necessary its length. For example if your object contains an array whose size is determined at run time, then you have that information somewhere in your program, so you can add that as a preamble to the data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900