Topic: How two I/O stream use the same buffer?


Author: Zijian Huang <zijian.huang@ncl.ac.uk>
Date: 10 Mar 1995 23:27:42 GMT
Raw View
I wrote programs in Borland C++ 4.0

about buffer of I/O streams

class mm
{
  char buf[80];
  strstream source;
  ifstream&   ifs;
.....
}

mm::mm(......) : source(buf, 80), ifs(....)
{
......
}

I use Turbo Debug to trace where think go wrong
then inside some of the member functions:

{......
  strcpy(buf,  "I love you");        // OK, the string was copied to buf
  strcpy(buf,  "way do");           //OK
  ifs.getline(buf, 70);      // system collapse, Window say "violate the memory management..."

Why, although strstream source use buf as buffer, system allow strcpy to write data into the buffer, of
course, buf is a quite independant char array, getline() should just write data directly to the char array,
just as strcpy() does.  So, when file stream try to write something into buf, it seem the system somehow
don't allow two stream use the same buffer. But it seem not to be the flavour of C language.

The similar things happen on iostream.

Is there any safe way to make two stream use the same buffer defined by an char array?

Thank you.