Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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?
 
#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 3-Nov-12 17:50pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Sizil Krishna at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Sizil Krishna at 4-Nov-12 9:59am
   
Thanks Pablo, i guess i will go with DB.

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

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 176
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100