Write Charset To File : char « Data Types « C++ Tutorial






/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <string>       // for strings
#include <iostream>     // for I/O
#include <fstream>      // for file I/O
#include <iomanip>      // for setw()
#include <cstdlib>      // for exit()
using namespace std;

// forward declarations
void writeCharsetToFile (const string& filename);
void outputFile (const string& filename);

int main ()
{
    writeCharsetToFile("charset.out");
    outputFile("charset.out");
}

void writeCharsetToFile (const string& filename)
{
    // open output file
    ofstream file(filename.c_str());

    // file opened?
    if (! file) {
        // NO, abort program
        cerr << "can't open output file \"" << filename << "\""
             << endl;
        exit(EXIT_FAILURE);
    }

    // write character set
    for (int i=32; i<256; i++) {
        file << "value: " << setw(3) << i << "   "
             << "char:  " << static_cast<char>(i) << endl;
    }

}   // closes file automatically

void outputFile (const string& filename)
{
    // open input file
    ifstream file(filename.c_str());

    // file opened?
    if (! file) {
        // NO, abort program
        cerr << "can't open input file \"" << filename << "\""
             << endl;
        exit(EXIT_FAILURE);
    }

    // copy file contents to cout
    char c;
    while (file.get(c)) {
        cout.put(c);
    }

}   // closes file automatically
value:  32   char:
value:  33   char:  !
value:  34   char:  "
value:  35   char:  #
value:  36   char:  $
value:  37   char:  %
value:  38   char:  &
value:  39   char:  '
value:  40   char:  (
value:  41   char:  )
value:  42   char:  *
value:  43   char:  +
value:  44   char:  ,
value:  45   char:  -
value:  46   char:  .
value:  47   char:  /
value:  48   char:  0
value:  49   char:  1
value:  50   char:  2
value:  51   char:  3
value:  52   char:  4
value:  53   char:  5
value:  54   char:  6
value:  55   char:  7
value:  56   char:  8
value:  57   char:  9
value:  58   char:  :
value:  59   char:  ;
value:  60   char:  <
value:  61   char:  =
value:  62   char:  >
value:  63   char:  ?
value:  64   char:  @
value:  65   char:  A
value:  66   char:  B
value:  67   char:  C
value:  68   char:  D
value:  69   char:  E
value:  70   char:  F
value:  71   char:  G
value:  72   char:  H
value:  73   char:  I
value:  74   char:  J
value:  75   char:  K
value:  76   char:  L
value:  77   char:  M
value:  78   char:  N
value:  79   char:  O
value:  80   char:  P
value:  81   char:  Q
value:  82   char:  R
value:  83   char:  S
value:  84   char:  T
value:  85   char:  U
value:  86   char:  V
value:  87   char:  W
value:  88   char:  X
value:  89   char:  Y
value:  90   char:  Z
value:  91   char:  [
value:  92   char:  \
value:  93   char:  ]
value:  94   char:  ^
value:  95   char:  _
value:  96   char:  '
value:  97   char:  a
value:  98   char:  b
value:  99   char:  c
value: 100   char:  d
value: 101   char:  e
value: 102   char:  f
value: 103   char:  g
value: 104   char:  h
value: 105   char:  i
value: 106   char:  j
value: 107   char:  k
value: 108   char:  l
value: 109   char:  m
value: 110   char:  n
value: 111   char:  o
value: 112   char:  p
value: 113   char:  q
value: 114   char:  r
value: 115   char:  s
value: 116   char:  t
value: 117   char:  u
value: 118   char:  v
value: 119   char:  w
value: 120   char:  x
value: 121   char:  y
value: 122   char:  z
value: 123   char:  {
value: 124   char:  |
value: 125   char:  }
value: 126   char:  ~
value: 127   char:  
value: 128   char:  €
value: 129   char:
value: 130   char:
value: 131   char:
value: 132   char:
value: 133   char:
value: 134   char:
value: 135   char:
value: 136   char:
value: 137   char:
value: 138   char:
value: 139   char:
value: 140   char:
value: 141   char:
value: 142   char:
value: 143   char:
value: 144   char:
value: 145   char:
value: 146   char:
value: 147   char:
value: 148   char:
value: 149   char:
value: 150   char:
value: 151   char:
value: 152   char:
value: 153   char:
value: 154   char:
value: 155   char:
value: 156   char:
value: 157   char:
value: 158   char:
value: 159   char:
value: 160   char:
value: 161   char:
value: 162   char:
value: 163   char:
value: 164   char:
value: 165   char:
value: 166   char:
value: 167   char:
value: 168   char:
value: 169   char:
value: 170   char:
value: 171   char:
value: 172   char:
value: 173   char:
value: 174   char:
value: 175   char:
value: 176   char:
value: 177   char:
value: 178   char:
value: 179   char:
value: 180   char:
value: 181   char:
value: 182   char:
value: 183   char:
value: 184   char:
value: 185   char:
value: 186   char:
value: 187   char:
value: 188   char:
value: 189   char:
value: 190   char:
value: 191   char:
value: 192   char:
value: 193   char:
value: 194   char:
value: 195   char:
value: 196   char:
value: 197   char:
value: 198   char:
value: 199   char:
value: 200   char:
value: 201   char:
value: 202   char:
value: 203   char:
value: 204   char:
value: 205   char:
value: 206   char:
value: 207   char:
value: 208   char:
value: 209   char:
value: 210   char:
value: 211   char:
value: 212   char:
value: 213   char:
value: 214   char:
value: 215   char:
value: 216   char:
value: 217   char:
value: 218   char:
value: 219   char:
value: 220   char:
value: 221   char:
value: 222   char:
value: 223   char:
value: 224   char:
value: 225   char:
value: 226   char:
value: 227   char:
value: 228   char:
value: 229   char:
value: 230   char:
value: 231   char:
value: 232   char:
value: 233   char:
value: 234   char:
value: 235   char:
value: 236   char:
value: 237   char:
value: 238   char:
value: 239   char:
value: 240   char:
value: 241   char:
value: 242   char:
value: 243   char:
value: 244   char:
value: 245   char:
value: 246   char:
value: 247   char:
value: 248   char:
value: 249   char:
value: 250   char:
value: 251   char:
value: 252   char:
value: 253   char:
value: 254   char:
value: 255   char:  ÿ








2.19.char
2.19.1.char and unsigned char
2.19.2.Write Charset To File
2.19.3.DO while loop: use char type variable to control
2.19.4.Using a nested if with char type