Table Of Contents
Previous Section Next Section

Program 101: Point of No Return

Why does the following program write out a correct file on UNIX and a bad one on Microsoft Windows? The program writes out 128 characters, but Microsoft Windows contains 129. Why?

  1 /*************************************************
  2  * Create a test file containing binary data.    *
  3  *************************************************/
  4 #include <iostream>
  5 #include <fstream>
  6 #include <stdlib.h>
  7
  8 int main()
  9 {
 10     // current character to write
 11     unsigned char cur_char;
 12
 13     // output file
 14     std::ofstream out_file;
 15
 16     out_file.open("test.out", std::ios::out);
 17     if (out_file.bad())
 18     {
 19         std::cerr << "Can not open output file\n";
 20         exit (8);
 21     }
 22
 23     for (cur_char = 0;
 24          cur_char < 128;
 25          ++cur_char)
 26     {
 27         out_file << cur_char;
 28     }
 29     return (0);
 30 }

(Next Hint 349. Answer 5.)

Table Of Contents
Previous Section Next Section