Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

stdiobuf Specialization of iostream for stdio FILEs


This section describes stdiobuf, which is a class which specializes a streambuf to dealwith the top level input/output structure FILE.

stdiobuf is intended to be used when mixing C and C++ code in the same program. New C++ code should use filebuf.


#include <iostream.h>
#include <stdiostream.h>
#include <stdio.h>

class stdiobuf : public streambuf

{

public:


stdiobuf(FILE* f);

FILE *

stdiofile();

virtual

virtual int
virtual int

˜stdiobuf();

overflow(int c=EOF);
pbackfail(int c);

virtual streampos


seekoff(streamoff, ios::seek_dir, int);

virtual
virtual int

int sync();
underflow();

};

class stdiostream : public ios

{

public:


stdiostream(FILE*);


˜stdiostream();

stdiobuf *

rdbuf();

};



Operations on a stdiobuf are reflected on the associated FILE. A stdiobuf is constructed in unbuffered mode, which causes all operations to be reflected immediately in the FILE. seekg()s and seekp()s are translated into fseek()s. setbuf() has its usual meaning; if it supplies a reserve area, buffering is turned back on.

In the following descriptions, assume that:

  • std is a stdiobuf.
  • sts is a stdiostream.
  • fp is a FILE *.

Constructors

stdiobuf(FILE * fp)

Constructs a stdiobuf in unbuffered mode, and associates it with fp.

stdiostream(FILE * fp)

Constructs a stdiostream, and associates it with fp.

stdiobuf members

FILE * fp = std.stdiofile()

Returns the file pointer connected to stdiobuf.

int l = std.overflow(int c)

Returns EOF if the file connected to stdiobuf is closed or c=EOF. Calls putc() and returns its value otherwise.

int l = std.pbackfail(int c)

Returns the value of ungetc().

streampos sp = std.seekoff(streamoff p, ios::seek_dir d, int l)

Parameter l is ignored. Returns the value returned by the associated fseek().

int l = std.sync()

Calls fflush() if the last operation was a write access. Returns fseek()’s return value for the current position.

int l = std.underflow()

Returns EOF if the file connected to stdiobuf is closed or end-of-file has been encountered. Returns the next character otherwise.

stdiostream member

stdiobuf * std = sts.rdbuf()

Returns a pointer to the stdiobuf connected to sts.

EXAMPLE

The following program opens the file #TEMP, attaches a variable of type stdiobuf to this file, and then prints a message to show if the stdiobuf is attached properly to the file.

#include <stdiostream.h>
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
int main()
{
  FILE *qw;
  const char * const name = "#TEMP";
  if (!(qw = fopen(name, "w")))
  {
    cerr << "Can't open " << name << ".\n";
    exit(1);
  }
  stdiobuf s(qw);
  FILE *rt = s.stdiofile();
  if (rt != qw)
  {
    cerr << "Error in stdiofile().\n";
  }
  else
  {
    cerr << "stdiofile() is working ok.\n";
  }
  return 0;
}

The result of executing the program is:

stdiofile() is working ok.
%  CCM0998 CPU time used: 0.0086 seconds

This program shows that the stdiofile() member function of stdiobuf returns the correct result in this case.

SEE ALSO


filebufistream, ostream, sbufpub