473,383 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

istringstream behavior

Since the strstream classes will be deprecated in the future, I have been looking for an alternative to this class for performing binary I/O. stringstream indicates that you can use rdbuf()->pubsetbuf(buf,size) to set the internal streambuf to point to your binary data and then allow you to perform normal stream read/writes. The example below works with GNU but doesn't work with Visual C++ or Sun studio. The old istrstream way works fine but can't seem to get istringstream to work the same way. If this isn't the way to perform binary I/O, I'm looking for alternatives. The only way I was able to get this example to work was to create a stringstream open for input/output, write the buffer with a write command and then read back with my read method.

#include <sstream>
#include <iostream>
#include <iomanip>

typedef struct {
float f;
int i;
double d;
void print( std::ostream& o ) const {
o<<"f="<<f<<" i="<<i<<" d="<<d<<std::endl;
}
void read( std::istream& in ) {
// assumes everything is aligned properly for example purposes
in.read((char*)&f, sizeof(float));
if (!in.good()) std::cout<<"read failed: f\n";
in.read((char*)&i, sizeof(int));
if (!in.good()) std::cout<<"read failed: i\n";
in.read((char*)&d, sizeof(double));
if (!in.good()) std::cout<<"read failed: d\n";
}
void write( std::ostream& os ) {
os.write((char*)&f, sizeof(float));
if (!os.good()) std::cout<<"write failed: f\n";
os.write((char*)&i, sizeof(int));
if (!os.good()) std::cout<<"write failed: i\n";
os.write((char*)&d, sizeof(double));
if (!os.good()) std::cout<<"write failed: d\n";
}
} str_t;

main() {

using namespace std;
str_t sti, sto;
sti.f=1.0;
sti.i=2;
sti.d=3.0;
istringstream s(ios::binary);
sti.print(cout);
cout<<"sizeof(str_t)="<<sizeof(str_t)<<endl;
cout<<"in_avail: " << s.rdbuf()->in_avail()<<endl;
s.rdbuf()->pubsetbuf( (char*)&sti, sizeof(str_t));
cout<<"in_avail: " << s.rdbuf()->in_avail()<<endl;
if (!s.good()) cout << "string buffer error\n";
sto.read( s );
if (!s.good()) cout << "string buffer error after read\n";
sto.print(cout);
return 0;
}
Sep 21 '06 #1
0 1794

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the...
3
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test...
7
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
4
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output...
6
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be...
8
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
6
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
11
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
7
by: mathieu | last post by:
Hello, Is there a way to construct a std::istringstream from a buffer of char and avoid copying the array of bytes ? const char s = "Hello, World"; std::string str(s); std::istringstream is;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.