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

C++ Programming Tutorial - C++ Strings

By David Bolton, About.com

3 of 9

Example 1 Continued

The fwrite() calls outputs the specified text. The 2nd and 3rd parameters are the size of the characters and the length of the string. Both are defined as being size_t which is unsigned integer. The result of this call is to write count items of the specified size. Note that with binary files, even though you are writing a string it does not append any carriage return or line feed characters. If you want those, you must explicitly include them in the string.

This is really a C example with a class wrapped around it. The example doesn't do much in the way of error checking or trapping and in practice you should always check success or do exception handling. Disks can get filled or not open successfully because they're already in use.

Back in an earlier C++ programming tutorial on Input and Output, I introduced iostream and showed how to do input from the keyboard and output to the screen. Part of the hierarchy of istream and ostream is iostream and fstream is derived from iostream. it's both the name of the include file and the class that implements it. Note that fstream's input functionality can be used with ifstream and output with a corresponding ofstream individually. These are the file equivalents of iostream and istream/ostream.

What this means is that you get open and close commands plus many commands inherited from both iostream. These are the other methods that we're interested in.

  • >> and <<
  • seekg and tellg - to move the get file pointer and read its position
  • seekp and tellp - to move the put file pointer and read its position
  • read and write - read and write blocks of data
On the next page : The C++ File Stream fstream
Explore C / C++ / C#
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.