Member Avatar for sonu_1

hi
i have problem , i want to modify a file without copy to another file so any one can give how to do it ? I tried following method to modify a given location or pointer in file but did not work .
ofstream outfile;
outfile.open ("test.txt");
outfile.seekp (3,ios::beg);
outfile<<5;
outfile.seekp (12,ios::beg);
outfile<<9;

Recommended Answers

All 7 Replies

When you said "did not work", please be specific. I know what happened, but that's only because I can deduce it due to experience. Not everyone may be able to immediately call from memory that opening a file in write mode will truncate the file to 0 bytes. You can open the file in read-write (aka. update) mode to get the correct behavior:

ofstream outfile("test.txt", ios::in | ios::out);

Note that this method only way that works is if you perfectly overwrite what's already there. If the overwriting data is shorter or longer by even a single character, it "doesn't work". ;)

Member Avatar for sonu_1

thanks but when i opened the file in input output bothmode like below
fstream iout ;
iout.open("test.txt");
than also not work .

than also not work

You did read the part where I said "does not work" is not helpful, right? If my suggestion doesn't work then the problem is elsewhere, and likely due to you failing to provide sufficient information about your problem.

Please read this and try again.

You cannot just open a file for read+write, seek to some position, write some data, and expect it to move all the rest of the file's data down - it will be overwritten... You have to write the rest of the file's data the from the location you are going to insert the new data to the position just past the length of the new data, and then re-seek to the that position and write the new data. This is a major PITA honestly, and the source of a LOT of data errors! The ONLY reason to NOT write to a second file is because you are short of disc space, because doing that is a lot easier and less error prone.

This is a good example of violating the KISS principle! :-)

Member Avatar for sonu_1

deceptikon
when file system is overwriting at given pointer location than file is truncated (the data what ever after the given pointer or seek postion ).there is any method to overwriting data which is shorter or longer .Below is my code if there is any correction than tell me .

Member Avatar for sonu_1
void state(int n)
{
    ifstream fin1,fin2;
    ofstream fout;
    fstream iout;
    fin1.open("student.txt");
    int a,j,ix[10],l=0,m=0,r,f,b,k1,loc,k;
    char ch[3],d[6];
    string s[10],c,st;
    for(j=0;j<n;j++)
    {
         k1=0;m=0;r=0;
        fin1.seekg(j*22+10,ios::beg);
        fin1.read(ch,2);
        c=ch;
        if(j==0){s[l++]=c;ix[l-1]=j;fout.open("st.txt");fout<<c<<" "<<j<<" "<<j<<" ";fout.close();}
        else
        {
        /* used for sorting purpose*/
         for(k=0;k<l;k++)
           {
               if(s[k]==c){m++;break;}// state is already stored in array or not
           }
           if(m==0)
           {
            s[l++]=c;ix[l-1]=j;
            fout.open("st.txt",ios::app);
             fout<<c<<"0 "<<"0 ";
             fout.close();
           }
           else
            {  iout.open("st.txt");
               while(r<j)
               {
               iout.seekg(r*7,ios::beg);
               iout.read(ch,2);
               st=ch;
               if(st==c)
               {
                iout>>f;
                iout>>b;
                loc=r;
               }
               r++;
              }
               iout.seekp(loc*7+3);
               iout<<j+1;
               iout.close();
               fout.open("st.txt",ios::app);
               fout<<st<<" "<<0<<" "<<loc+1<<" ";
               fout.close();
            }
          }
      }
    for(k=0;k<l;k++)
    {
        for(j=0;j<l-k-1;j++)
        {
            if(s[j]>s[j+1])
            {
                swap(s[j],s[j+1]);
                swap(ix[j],ix[j+1]);
            }
        }
    }
    fout.open("state.txt");
    fin2.open("st.txt");
    for(k=0;k<l;k++)
    {
        fout<<s[k]<<" ";
        fout<<ix[k]<<" ";
        fin2.seekg(ix[k]*7+3,ios::beg);
        fin2>>f;
          while(f!=0)
            {   fout<<(f-1)<<" ";
                fin2.seekg((f-1)+3,ios::beg);
                fin2>>f;
            }
    }
    fout.close();
    fin2.close();
}

there is any method to overwriting data which is shorter or longer

No. If you're not overwriting data of exactly the same length, you have little choice but to create a temporary file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.