473,569 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

istringstream behavior

2 New Member
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::en dl;
}
void read( std::istream& in ) {
// assumes everything is aligned properly for example purposes
in.read((char*) &f, sizeof(float));
if (!in.good()) std::cout<<"rea d failed: f\n";
in.read((char*) &i, sizeof(int));
if (!in.good()) std::cout<<"rea d failed: i\n";
in.read((char*) &d, sizeof(double)) ;
if (!in.good()) std::cout<<"rea d failed: d\n";
}
void write( std::ostream& os ) {
os.write((char* )&f, sizeof(float));
if (!os.good()) std::cout<<"wri te failed: f\n";
os.write((char* )&i, sizeof(int));
if (!os.good()) std::cout<<"wri te failed: i\n";
os.write((char* )&d, sizeof(double)) ;
if (!os.good()) std::cout<<"wri te 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(s tr_t)="<<sizeof (str_t)<<endl;
cout<<"in_avail : " << s.rdbuf()->in_avail()<<en dl;
s.rdbuf()->pubsetbuf( (char*)&sti, sizeof(str_t));
cout<<"in_avail : " << s.rdbuf()->in_avail()<<en dl;
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 1828

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

Similar topics

1
3420
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 allocated memory correctly? Or am I missing something glaringly simple? Thanks in advance, S. Armondi std::istringstream** ArgStream;
3
4722
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 string"); cout << ist.str() << endl;
7
748
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 encounters the two byte shorts, it either thinks it has reached the null terminator? or eof and consequently stops reading values back in. It doesn't...
4
2317
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 data. The data comes in string lines like "OREBlegQ 14854 731.818" which need to be split into a string, int and double when stored in the class....
6
2361
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 unmodified... However when I try to pass it fn(istringstream s) // doesn't compile but fn(istringstream & s) // does.
8
2079
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
6
2984
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
2887
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
3168
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; is.str( str );
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.