Overwriting Data in a C++ File using fstream
Hi i want to overwrite the content(object) in a specif开发者_运维知识库ic file i have set the position but it always add to the end of the file
Code
int InputIO::editPatient(int location,Obj P){
int positon=location*sizeof(P);
f.open("File.dat",ios::in|ios::out|ios::app|ios::binary|ios::ate);
f.seekp(0,ios::beg);
f.seekp(positon,ios::cur);
f.write((char*)&P,sizeof(Movie));
f.close();
return 0;
}
Don't use the ios::app
flag (which stands for append). When you use this flag, it prevents you from reading or writing any of the content that was present in the file before you opened it -- you can only add new content to the end (and since you opened in read+write mode, you can re-read what you wrote and rewrite it, but you still can't get to the data before it).
Just solve this have to remove ios::app (Append) Append always add to the end of the file
精彩评论