There are a number of problems here. First, updatedata
is a
std::ifstream
, which means that it doesn’t have functions like write
or seekp
. Second, you’ve opened it in text mode, so you cannot seek
to an arbitrary position in the file; you can only seek to the beginning
or the end, or to a position which was returned by tellg
or tellp
.
(It’s undefined behavior otherwise.) You’ll have to memorize the
position before each read
, and work from that.) Third, you don’t show
the definition of account
, but in general, you cannot use
istream::read
and ostream::write
directly on objects of the type:
you have to format the output and parse the input, using an intermediate
buffer.
EDIT:
I’ve just noticed that you actually open the file a second time for
writing. This cannot be made to work for several reasons, not the least
of which is that some systems will not allow opening a file with write
access if the file is already open elsewhere. Other than that: you open
with std::ios_base::app
, which means that all writes will append to
the end of the file, regardless of where the position marker was before.
In summary:
- You need to use an
std::fstream
. - You need to memorize the position (using
tellg
) before each read,
and seek back to it if you want to write. - You need to use an intermediate buffer for the data. (This may not
be necessary if 1) the program which reads and writes the data is
compiled with the same compiler, using the same options, on the same
machine, running under the same OS, and 2) the data structure is a pure
POD. These conditions are almost never met in well written C++.)
solved How can I update record in file in c++?