In modern terms we can call a binary file a stream of bytes and more modern languages tend to work with streams rather than files. The important part is the data rather than where it came from! This example shows that you can write text to a binary file.
RandomAccess ra( filename) ;
if ( ra.OpenWrite() )
{
if (!ra.Write( mytext ))
cout << "Failed to write to file " << filename << endl;
ra.Close() ;
}
else
cout << "Failed to open " << filename << " fior writing " << endl;
This uses the class RandomAccess to open a binary file for writing, then writes a string into it. The RandomAccess class uses a FILE to do the main work. It's opened in "wb " mode (refer to the C tutorial for more information on that) and then writes the text to the file. It's actually writing sequentially though it could be made to write anywhere in the file. However, because the "wb" mode creates the file (or deletes all content of an existing file) there isn't much point.
If you try to move the file pointer to a place in the file that doesn't exist, the results aren't defined. It might create a file to fill in the gaps or it might just not work. That depends upon the operating system so if you want to write software that is portable don't use it!
On the next page : How Example 1 Works

