14.8. Writing Data Randomly to a Random-Access File

Figure 14.12 writes data to the file credit.dat and uses the combination of fstream functions seekp and write to store data at exact locations in the file. Function seekp sets the put file-position pointer to a specific position in the file, then function write outputs the data. Line 6 includes the header ClientData.h defined in Fig. 14.9, so the program can use ClientData objects.

 1   // Fig. 14.12: Fig14_12.cpp 2   // Writing to a random-access file. 3   #include <iostream> 4   #include <fstream> 5   #include <cstdlib> 6   #include "ClientData.h" // ClientData class definition 7   using namespace std; 8  9   int main()10   {11      int accountNumber;12      string ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.