I have a huge file that has already been created. I need to write some data at the beginning of the file, while preserving the other contents of the file. The following code distorts an existing file. Can someone help me with the correct method.
ofstream oFile(FileName,ios::out|ios::binary);
oFile.seekp(0);
oFile.write((char*)&i,sizeof(i));
oFile.write((char*)&j,sizeof(i));
oFile.close();
EDIT: Basically, I want to rewrite a few bytes of an existing file in different places, including start. I know the byte address of the locations to write. My record will not resize the file.
I need to do something equivalent to the following code that works:
int mode = O_RDWR;
int myFilDes = open (FileName, mode, S_IRUSR | S_IWUSR);
lseek (myFilDes, 0, SEEK_SET);
write (myFilDes, &i, sizeof (i));
write (myFilDes, &j, sizeof (j));
source
share