3. Writing, Compiling, and Debugging Simple Programs

Chapter 18 - Complete I/O in C++

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Binary Files
Most of the example programs presented so far have used standard text files—or streams, as they are more appropriately called. This is not surprising since streams were originally designed for text, and text therefore is their default I/O mode.
Standard text files, or streams, contain a sequence of characters including carriage returns and linefeeds. In text mode, there is no requirement that individual characters remain unaltered as they are written to or read from a file. This can cause problems for certain types of applications. For example, the ASCII value for the newline character is a decimal 10. However, it could also be written as an 8-bit, hexadecimal 0A. In both C and C++ programs, it is considered to be the single character constant ‘\n’.
Under MS-DOS-compatible operations, the newline character is physically represented as a character pair—carriage return (decimal 13)/linefeed (decimal 10). Normally, this isn’t a problem since the program automatically maps the two-character sequence into the single newline character on input, reversing the sequence on output. The problem is that a newline character occupies 1 byte, while the CR/LF pair occupies 2 bytes of storage.
Binary files, or streams, contain a sequence of bytes with a one-to-one correspondence to the sequence found in the external device (disk, tape, or terminal). In a binary file, no character translations will occur. For this reason, the number of bytes read or written will be the same as that found in the external device.
When an application is developed that needs to read an executable file, the file should be read as a binary file. Likewise, binary files should be used when reading or writing pure data files, like databases. Performing this action guarantees that no alteration of the data occurs except those changes performed explicitly by the application.
The following program is identical to OSTRM.CPP, described earlier in this chapter, except that the output file mode has been changed from text to ios::binary:
//
// binary.cpp
// This program is a modification of OSTRM.CPP and
// demonstrates binary file output.
// Copyright (c) Chris H. Pappas and William H. Murray, 1998
// Valid ofstream member functions include:
//         ofstream::open     ofstream::rdbuf
// Valid ostream member functions include:
//         ostream::flush     ostream::ostream
//         ostream::put       ostream::seekp
//         ostream::tellp     ostream::write
#include <fstream.h>
#include <string.h>
#define iSTRING_MAX 40

void main(void)
{
 int i=0;
 long ltellp;
 char pszString[iSTRING_MAX] = “Sample test string\n”;
 // file opened in binary mode!
 ofstream ofMyOutputStream(“MYOSTRM.OUT”,ios::out | ios::binary);

 // write string out character by character
 // notice that ‘\n’ is NOT translated into 2 characters!
 while(pszString[i] != ‘\0’) {
   ofMyOutputStream.put(pszString[i]);
   ltellp = ofMyOutputStream.tellp( );
   cout << “\ntellp value: ” << ltellp;
   i++;
 }
 // write entire string out with write member function
 ltellp = ofMyOutputStream.tellp( );
 cout << “\ntellp’s value before writing 2nd string: ” << ltellp;
 ofMyOutputStream.write(pszString,strlen(pszString));
 ltellp = ofMyOutputStream.tellp( );
 cout << “\ntellp’s updated value: ” << ltellp;

 ofMyOutputStream.close( );

}
The abbreviated output, seen in the following listing, illustrates the one-to-one relationship between a file and the data’s internal representation:
tellp value: 1
tellp value: 2
tellp value: 3
    .
    .
    .
tellp value: 17
tellp value: 18
tellp value: 19
tellp’s value before writing 2nd string: 19
tellp’s updated value: 38
The string pszString, which has 19 characters plus a ‘\0’ null string terminator, is output exactly as stored, without the appended ‘\0’ null terminator. This explains why tellp( ) reports a multiple of 19 at the completion of each string’s output.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net