For Programmers: Free Programming Magazines  


Home > Archive > VC STL > March 2005 > strstream doesn't seem to support iostream interfaces?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author strstream doesn't seem to support iostream interfaces?
mc@pochta.ws

2005-03-20, 4:03 pm

Hello,

I'm trying to use "iostream" as an input parameter, allowing thereby a
user to specify, for example a file stream ("fstream") or memory stream
("strstream"). With "fstream" all seems to work fine, while "strstream"
isn't that case. I've managed to get a sample code describing the
problem I'm experiencing (Visual Studio 2003, empty console
application). Any ideas appreciated!


#include <iostream>
#include <fstream>
#include <strstream>
using namespace std;

void Test(iostream* DataStream)
{
int i = 6347;

DataStream->sp(0);
DataStream->write((const char*)&i, 4);

int j = 0;

DataStream->sp(0);
DataStream->read((char*)&j, 4);
}

int main(int argc, char* argv[])
{
strstream sx;
//Test(&sx); // j == 0 (why???)

int i = 6347;

((iostream*)&sx)->sp(0);
((iostream*)&sx)->write((const char*)&i, 4);

int j = 0;

((iostream*)&sx)->sp(0);
((iostream*)&sx)->read((char*)&j, 4);

fstream fx;
fx.open("temp", ios_base::in | ios_base::out | ios_base::binary |
ios_base::trunc);
Test(&fx); // j == 6347
}

Tom Widmer

2005-03-21, 8:59 am

mc@pochta.ws wrote:
> Hello,
>
> I'm trying to use "iostream" as an input parameter, allowing thereby a
> user to specify, for example a file stream ("fstream") or memory stream
> ("strstream"). With "fstream" all seems to work fine, while "strstream"
> isn't that case. I've managed to get a sample code describing the
> problem I'm experiencing (Visual Studio 2003, empty console
> application). Any ideas appreciated!


Memory streams are more fully featured than file streams - they maintain
a put position and a separate read position.

> #include <iostream>
> #include <fstream>
> #include <strstream>


Note strstream is deprecated - prefer <sstream> and stringstream.

> using namespace std;
>
> void Test(iostream* DataStream)
> {
> int i = 6347;
>
> DataStream->sp(0);
> DataStream->write((const char*)&i, 4);
>
> int j = 0;
>
> DataStream->sp(0);


That's your problem. Should be:
DataStream->sg(0); //same for fstream, different for stringstream

> DataStream->read((char*)&j, 4);
> }


Tom
David Lowndes

2005-03-21, 8:59 am

>I'm trying to use "iostream" as an input parameter, allowing thereby a
>user to specify, for example a file stream ("fstream") or memory stream
>("strstream"). With "fstream" all seems to work fine, while "strstream"
>isn't that case. I've managed to get a sample code describing the
>problem I'm experiencing (Visual Studio 2003, empty console
>application). Any ideas appreciated!


Try adding a call to clear:

void Test(iostream* DataStream)
{
int i = 6347;

DataStream->sp(0);
DataStream->clear();
DataStream->write((const char*)&i, 4);

int j = 0;

> DataStream->sp(0);
>should be:

DataStream->sg(0);
DataStream->read((char*)&j, 4);

I don't understand why, but the string stream sets the internal
failure flag on the initial output s.

Dave
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com