edit a binary file

hi all,
i've been trying to edit a binary file so can i use seekp to be at the position where iwant to edit and then use put() to edit or this will just add a new charcter rather than editing that character
??????????
It will overwrite what is already there. You can't insert characters into the middle of a file. You would have to manually shift everything up to make room.
@Galik
what i mean is that if there was a character ( c ) in that position i want to make it any other one (z) for example. i don't want to insert z before or after the charcter (c )
so it would make what????
tanks for your reply
As I said, it won't insert the z it will change the c that is already there.
i have tried to do as i said i made an array of character containing just 'a' then using seekp() i tried to modify its contents but it didn't work

here is the code:

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{char h[10];
char c;
for(int i=0; i<10; i++)
h[i]='a';
ofstream out("file2", ios::out |ios::binary);
if(!out){cout<<"cannot open file.\n";}
out.write((char *) &h, sizeof h );
out.seekp(5,ios::beg);
c='z';
out.put(c);
ifstream in("file2", ios::in | ios::binary);
if(!in){cout<<"cannot open file "<<endl;}
while(!in.eof()){
in.get(c);
cout<<c<<endl;
}


system("PAUSE");
return EXIT_SUCCESS;
}

it should give a a a a a z a a a a
but it give a a a a a a a a a a

I think you need to close your output file before you open it for input. It probably has not made the change on disk because its still waiting in the buffer.

Try adding out.close(); after you made your changes and before you open the file for input.
@Galik
thanks so much
Topic archived. No new replies allowed.