使用fstream覆盖C++文件中的数据

Sud*_*ha 1 c++ visual-c++

嗨,我想覆盖特定文件中的内容(对象)我已设置位置,但它总是添加到文件的末尾

 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;



        }
Run Code Online (Sandbox Code Playgroud)

Ada*_*eld 5

不要使用ios::app标志(代表追加).当您使用此标志时,它会阻止您在打开文件之前读取或写入文件中存在的任何内容 - 您只能添加新内容到最后(并且因为您以读取+写入模式打开,可以重新读取你写的内容并重写它,但你仍然无法获取它之前的数据).