Previously, these functions were not required. The file opened to read, the data was recorded in the vector, then the record was removed in the vector and the file was overwritten.
Now the file is open for reading, writing and duplication. And for the algorithm described above, you need to use the Seekg
function, Seekp
to install the cursor in the desired position. Here is the method, what’s wrong?
void dbtextfileadapter :: remove (String Login)
{
File- & gt; Seekg (IOS_BASE :: Beg);
While (* File & GT; & gt; user._login & gt; & gt; user._password)
{
Container.Push_Back (User);
}
For (vector & lt; User & gt; :: iterator it = container.begin (); it! = Container.end (); ++ IT)
{
if (IT- & gt; _login == login) // Find recording
{
Container.ERASE (IT); // Delete the record
Break;
}
}
File- & gt; Seekp (iOS_BASE :: Beg); //this does not work
For (vector & lt; User & gt; :: iterator i = container.begin (); i! = container.end (); ++ i)
{
* File & lt; & lt; i- & gt; _login & lt; & lt; "" & lt; & lt; i- & gt; _password & lt; & lt; Endl; // Overwriting
}
container.clear ();
}
Answer 1, Authority 100%
First, the call
file- & gt; seekp (ios_base :: beg);
is formally not easily. This feature has two versions
basic_ostream & amp; Seekp (POS_Type POS);
Basic_ostream & amp; Seekp (Off_type off, std :: ios_base :: Seekdir dir);
ios_base :: Beg
This is the value of type iOS_BASE :: Seekdir
, which can only be the second parameter of the second version of the method. You are in the first parameter, thereby causing the first version of the method, but applying it to ios_base :: Beg
as an absolute position. It turns out to be convertible to the type POS_TYPE
, i.e. The code is compiled, but what exactly it turns out to be as a result – who knows it …
The same problem takes place with
file- & gt; seekg (ios_base :: beg);
right
file- & gt; seekg (0, ios_base :: beg);
...
File- & gt; Seekp (0, iOS_BASE :: Beg);
or just
file- & gt; seekg (0);
...
File- & gt; Seekp (0);
Secondly, the reading cycle is over with a stream in the fail ()
state (exhibited Failbit
and Eofbit
). While the fail () state
is not reset, i.e. While the thread is not translated into the Good ()
state, it will ignore all I / O operations. Make
file- & gt; clear ();
Before moving to the record and only after that, already Seekp
, etc.