1. Home
  2. Computing & Technology
  3. C / C++ / C#

C++ Programming Tutorial - C++ Strings

By , About.com Guide

9 of 9

More on Example 3

Reading a Record

This is done in MyFile::Read(int index). It looks up the indextype with
indexvector[index]

This lets you treat a vector just like an array and returns an indextype struct. Because the fstream reads data into chars, the class member buffer is used as a temporary buffer. It is resized with each read which is not very efficient. If you know the maximum size of data that will be read it could be created big enough just once. After reading the record into memory, a null character \0 is appended to turn it into a proper c-string. Don't forget it or you'll get a string with lots of rubbish on the end.

To test that it works correctly, the data file is opened for reading and a record read in. As the 1000 strings were written with a string length = 5 + n where n run from 0 to 1000. Record x should have a length = x + 5.

This completes this C++ programming tutorial on random access files. The next one will be on text files.

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. More on Example 3

©2009 About.com, a part of The New York Times Company.

All rights reserved.