home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 099 / IOSTREAM.ZIP / HEADER.H < prev    next >
C/C++ Source or Header  |  1993-01-07  |  6KB  |  273 lines

  1. // header.h
  2. // for iostream tutorial by Eric Nagler
  3. //
  4. ////////////////////////////////////
  5. // My private header file for C++ programs
  6. ////////////////////////////////////
  7.  
  8. // There's also a #define ZORTECH in my Zortech header file so that I can
  9. // write different statements to print addresses.
  10.  
  11. #define BORLAND
  12.  
  13. // OLD is defined in the cc.bat file which I use to compile Turbo C++
  14. // using the old (version 1.2) style I/O. Therefore, use <stream.h>
  15.  
  16. #ifdef OLD
  17. #include <stream.h>
  18.  
  19. // Otherwise, it must be version 2.0, so use <fstream.h>, which also
  20. // includes <iostream.h>
  21.  
  22. #else
  23. #include <fstream.h>
  24.  
  25. // Any manipulators taking a parameter will also be accommodated
  26.  
  27. #include <iomanip.h>
  28. #endif
  29.  
  30. // I also need console I/O for the PAUSE() function
  31.  
  32. #include <conio.h>
  33.  
  34. // Include <stdio.h> for C style I/O
  35.  
  36. #include <stdio.h>
  37.  
  38. // Include these because they're used so frequently, and really don't add
  39. // that much to the compilation time
  40.  
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include <stdlib.h>
  44. #include <time.h>
  45.  
  46. ///////////////////////////////////
  47.  
  48. // My private defines
  49.  
  50. #define FALSE               0
  51. #define TRUE                !FALSE
  52. #define AND                 &&
  53. #define OR                  ||
  54. #define NO                  0
  55. #define YES                 !NO
  56. #define EQUALS              ==
  57. #define IS_EQUAL_TO         ==
  58. #define NOT                 !
  59. #define IS_NOT_EQUAL_TO     !=
  60. #define NOT_EQUAL_TO        !=
  61. //
  62. #define BLANK               ' '
  63. #define SPACE               ' '
  64. #define ASTERISK            '*'
  65. #define DECIMAL             '.'
  66. #define NEW_LINE            '\n'
  67. #define NUL                 '\0'
  68. #define TAB                 '\t'
  69. #define BACKSPACE           '\b'
  70. #define BEEP                '\a'
  71. #define FORMFEED            '\f'
  72. #define RETURN              '\r'
  73.  
  74. ///////////////////////////////////
  75.  
  76. // My private PAUSE function
  77.  
  78. void PAUSE()
  79. {
  80.    cout << "Press any key to "
  81.         << "continue...\n" ;
  82.    getch() ;
  83. }
  84.  
  85. // My private flush-the-input-buffer manipulator until either EOF or
  86. // new-line is found.
  87.  
  88. istream& FLUSH(istream& strm)
  89. {
  90.    strm.clear() ;
  91.    char ch ;
  92.    while (!strm.get(ch).eof() AND ch != NEW_LINE)
  93.                          ;  // Empty body
  94.    return strm ;
  95. }
  96.  
  97. ////////////////////////////////////
  98. // This function prints the various I/O flags of an input stream.
  99. // Defaults to 'cin'
  100.  
  101. void IOFLAGS(istream& stream=cin)
  102. {
  103.    cout << "eof state is  " << (stream.eof()
  104.            ? "ON" : "OFF") << endl ;
  105.    cout << "good state is " << (stream.good()
  106.            ? "ON" : "OFF") << endl ;
  107.    cout << "fail state is " << (stream.fail()
  108.            ? "ON" : "OFF") << endl ;
  109.    cout << "bad state is  " << (stream.bad()
  110.            ? "ON" : "OFF") << endl ;
  111.    cout << "if (!instance) returns "<< (!stream ? "TRUE" : "FALSE") << endl ;
  112.    cout << "if (instance) returns " << (stream ? "TRUE" : "FALSE") << endl ;
  113. }
  114.  
  115. /////////////////////////////////////
  116. // This function prints the format flags of an output stream.
  117. // Defaults to 'cout'
  118.  
  119. void FORMATFLAGS(ostream& stream=cout)
  120. {
  121.    static char* message[] =
  122.    {
  123.      "skipws" ,
  124.      "left" ,
  125.      "right" ,
  126.      "internal" ,
  127.      "dec" ,
  128.      "oct" ,
  129.      "hex" ,
  130.      "showbase" ,
  131.      "showpoint" ,
  132.      "uppercase" ,
  133.      "showpos" ,
  134.      "scientific" ,
  135.      "fixed" ,
  136.      "unitbuf" ,
  137.      "stdio"
  138.    } ;
  139.  
  140.    cout << "FORMAT FLAGS\n" ;
  141.    long f = stream.flags() ;
  142.    for (int i = 0 ; i < 16 ; ++i)
  143.    {
  144.      if (f & 0x0001)
  145.        cout << message[i] << endl ;
  146.      f >>= 1 ;
  147.    }
  148. }
  149.  
  150. ///////////////////////////////////
  151.  
  152. // Define a global instance of the
  153. // class 'ofstream' called POUT that
  154. // is tied to the printer
  155.  
  156. ofstream POUT("prn") ;
  157.  
  158. /////////////////////////////////////
  159.  
  160. // A manipulator that directs to the printer all subsequent output for
  161. // the one statement in which it is found
  162.  
  163. ostream& PRINTER(ostream&)
  164. {
  165.    return POUT ;
  166. }
  167.  
  168. // A manipulator that directs to the screen all subsequent output for
  169. // the one statement in which it is found
  170.  
  171. ostream& SCREEN(ostream&)
  172. {
  173.    return cout ;
  174. }
  175.  
  176. // A manipluator to set left justification
  177.  
  178. ostream& LEFT(ostream& str)
  179. {
  180.    str.setf(ios::left , ios::adjustfield) ;
  181.    return str ;
  182. }
  183.  
  184. // A manipluator to set right justification
  185.  
  186. ostream& RIGHT(ostream& str)
  187. {
  188.    str.setf(ios::right , ios::adjustfield) ;
  189.    return str ;
  190. }
  191.  
  192. // A manipluator to set internal justification
  193.  
  194. ostream& INTERNAL(ostream& str)
  195. {
  196.    str.setf(ios::internal , ios::adjustfield) ;
  197.    return str ;
  198. }
  199.  
  200. // A manipulator to show the decimal point
  201.  
  202. ostream& SHOWPOINT(ostream& str)
  203. {
  204.    str.setf(ios::showpoint) ;
  205.    return str ;
  206. }
  207.  
  208. // A manipulator to show the base on hex and octal output
  209.  
  210. ostream& SHOWBASE(ostream& str)
  211. {
  212.    str.setf(ios::showbase) ;
  213.    return str ;
  214. }
  215.  
  216. // A manipulator to suppress the showing of the base
  217.  
  218. ostream& NOSHOWBASE(ostream& str)
  219. {
  220.    str.unsetf(ios::showbase) ;
  221.    return str ;
  222. }
  223.  
  224. // A manipulator to show fixed point output
  225.  
  226. ostream& FIXED(ostream& str)
  227. {
  228.    str.setf(ios::fixed , ios::floatfield) ;
  229.    return str ;
  230. }
  231.  
  232. // A manipulator to show scientific point output
  233.  
  234. ostream& SCIENTIFIC(ostream& str)
  235. {
  236.    str.setf(ios::scientific , ios::floatfield) ;
  237.    return str ;
  238. }
  239.  
  240. // A manipulator to show uppercase output on hex and scientific
  241. // numbers
  242.  
  243. ostream& UPPERCASE(ostream& str)
  244. {
  245.    str.setf(ios::uppercase) ;
  246.    return str ;
  247. }
  248.  
  249. // A manipulator to show lowercase output on hex and scientific
  250. // numbers
  251.  
  252. ostream& LOWERCASE(ostream& str)
  253. {
  254.    str.unsetf(ios::uppercase) ;
  255.    return str ;
  256. }
  257.  
  258. // A manipulator to show a '+' on positive numbers
  259.  
  260. ostream& SHOWPOS(ostream& str)
  261. {
  262.    str.setf(ios::showpos) ;
  263.    return str ;
  264. }
  265.  
  266. // A manipulator to negate showing a '+' on positive numbers
  267.  
  268. ostream& NOSHOWPOS(ostream& str)
  269. {
  270.    str.unsetf(ios::showpos) ;
  271.    return str ;
  272. }
  273.