Overwriting data in a C ++ file using fstream

Hi, I want to rewrite the contents (object) in a specific file, I set the position, but 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; } 
+1
source share
2 answers

Just decide what to remove ios :: app (Append). Add always add to the end of the file

+1
source

Do not use the ios::app flag (which means adding). When you use this flag, it prevents you from reading or writing any content that was in the file before opening it - you can only add new content to the end (and since you opened it in read and write mode, you can re-read what you wrote, and rewrite it, but you still can’t get to the data).

+5
source

Source: https://habr.com/ru/post/1332900/


All Articles