Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - streams: constructors http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   Streams: Constructors
   Class ostream has three constructors. The first takes as its  argument
   a  pointer to a streambuf, this is another class in the I/O set  which
   we will discuss shortly. Streambufs provide buffering. Thus:

   streambuf sb;
   ostream os(&sb);
   The  second constructor takes a file descriptor as its  argument,  and
   creates an ostream whose output is directed to the corresponding file.
   Output  to a file is untranslated, so if text is being output  via  an
   ostream of this type, explicit carriage return line feed pairs must be
   included in the output. For example:

   #include <stream.hpp>
   #include <io.h>

   main()
   {
   int fd = creat("JACKJILL.TXT",0);
   ostream jj(fd);
   for (int i = 20; i--; jj << "Went up the hill\r\n") ;
   }
   The  final  constructor takes an integer length argument, and  a  char
   pointer to an area of memory to be used as a buffer. This buffer could
   be used for martialling smaller fragments of text in much the same way
   as  sprintf  . In the case of the ostream we do not have  to  pay  any
   attention to overflowing the buffer. An ostream knows when its  buffer
   is full. So we could have:

   #include <stream.hpp>

   char *p = "The quick brown fox jumped over ",
   *q = " lazy dogs only ",
   *r = " weeks after attempting the world record";

   main()

   {
   char buf[71];
   buf[70] = '\0';
   ostream os(70,buf);
   os << p << 22 << q << 3 << r;
   puts(buf);
   }
   This runcates the message without causing any harm.
   Standard Streams
   Including  stream.hpp  in a C++ program  automatically  makes  several
   standard  ostreams available to the programmer. With Zortech  C++  2.0
   these are:
   // stdout - standard output,
   // stderr - standard error output,
   // stdprn - LPT1, the printer,
   // stdaux - COM1, the serial port.

   Output to these standard streams may be intermixed freely with  output
   via  printf, puts, fprintf and fputs etc. In fact the standard  output
   streams  are  simply  a  protective  layer  round  the  stdio   output
   functions. cout and printf share the same output buffer. If this  were
   not  so,  it would be impossible to guarantee that output  via  either
   technique would appear in the order in which it was sent.


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson