Previously, these functions were not required. The file was opened for reading, the data was written to the vector, then the record was deleted in the vector and the file was overwritten.

Now the file is open for reading, writing and recording. And to carry out the algorithm described above, you need to use the functions seekg , seekp to set the cursor to the desired position. Here's the method, what's wrong?

 void DBTextFileAdapter::remove(string login) { file->seekg(ios_base::beg); while (*file >> user._login >> user._password) { container.push_back(user); } for (vector<User>::iterator it = container.begin(); it != container.end(); ++it) { if (it->_login == login) //находим запись { container.erase(it); //удаляем запись break; } } file->seekp(ios_base::beg); //это не работает for (vector<User>::iterator i = container.begin(); i != container.end(); ++i) { *file << i->_login << " " << i->_password << endl; //перезапись } container.clear(); } 
  • You want to say that before you did not reinstall the current position of the file to the beginning, and he himself reinstalled there? It looks kind of weird. - VladD
  • I previously read the file using ifstream, closed it. Then he opened with the help of ofstream and closed, the positions were constantly from the beginning of the file. And the seek .. () methods were not required. Now, as I explained, the fstream object and these methods are supposed to be used. - Spaceman
  • What is "it does not work"? What is it expressed in? - AnT
  • @Spaceman: Okay, this is important. Add this to the question. (Well, explain what “does not work” is expressed, yes.) - VladD
  • But you still have to edit the file size anyway (truncate or what axis do you have?). So, the initial decision (with re-opening for writing) was better (KISS principle). - avp

1 answer 1

First, the challenge

 file->seekp(ios_base::beg); 

not formally legal. This feature has two versions.

 basic_ostream& seekp( pos_type pos ); basic_ostream& seekp( off_type off, std::ios_base::seekdir dir); 

ios_base::beg is a value of type ios_base::seekdir , which can only be the second parameter of the second version of the method. You bust it in the first parameter, thereby calling the first version of the method, but slipping to it the value ios_base::beg as an absolute position. It turns out to be convertible to pos_type , i.e. the code is compiled, but what exactly it turns out is equal to the result - who knows ...

The same problem occurs with

 file->seekg(ios_base::beg); 

Right

 file->seekg(0, ios_base::beg); ... file->seekp(0, ios_base::beg); 

or simply

 file->seekg(0); ... file->seekp(0); 

Secondly, the reading cycle ended with the flow in the fail() state (set failbit and eofbit ). Until the fail() state is reset, i.e. until the thread is in the good() state, it will ignore all I / O operations. Make a

 file->clear(); 

before proceeding to the record and only after that seekp , etc.

  • this is all fine, but the dubbing is not done with your edits. - Spaceman
  • @Spaceman: Added a "second" in response. - AnT