• Create Account

## STL Streams for files and buffers

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1Khatharr  Members

7732
Like
0Likes
Like

Posted 20 December 2012 - 06:53 PM

So I'm reworking my Input Manager and it's coming along nicely. I've written a few functions that pass around basic_ofstream / basic_ifstream for saving and loading the key mappings. Those are working great but I'd like to be able to save/load to/from a memory buffer as well. For some reason all my STL documentation is either too referential to be of use for finding out what to use in a specific case or else too 'for dummies' style to mention cases where something unusual is being done.

I'm using unsigned char as the element type here since it's the most appropriate for the data I'm saving, so stringstream doesn't seem right, but the more I look at this gibberish the more confused I become, so I thought I'd ask here.

Is there a way (and if so, how) to set up an input stream that will write unsigned char data into a buffer which will expand as data is added?

Is it possible that I could use a single STL type in the underlying system and then have the pair of top-level functions feed the system either an ofstream or a buffer-stream? (polymorphism)

I mean I guess if it comes down to it I can create a new helper type that will figure it out one way or the other, but I get the feeling that underneath all this "thing&<derp>::moocow<wtf>&&&" madness this is something that STL can do easily.

Any advice would be greatly appreciated. Thank you.

Edit - Also, I wonder if I just have a really bad collection of docs here. Does anyone have any suggestions for somewhere I can get some good STL docs that explain what parts are actually intended to be used and what parts are just implementation or alien conspiracy?

Edited by Khatharr, 20 December 2012 - 06:57 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #2SiCrane  Moderators

11530
Like
0Likes
Like

Posted 20 December 2012 - 07:59 PM

Chances are a std::stringstream will work just fine. Just change the type you are passing around from ifstream/ofstream to istream/ostream. If you're feeling adventurous you can use a std::strstream, which allows you to manually manage the memory the stream writes to, but it's somewhat inconvenient from my point of view.

As for documentation, a good reference for the standard library is Nicolai Josuttis' "The C++ Standard Library" which covers how the library is meant to be used in addition to what the different bits do. I've only used the first edition myself, but there is a second edition out now that covers C++11. I personally mostly consult the C++ standard itself if I need to look things up.

### #3Khatharr  Members

7732
Like
0Likes
Like

Posted 21 December 2012 - 04:30 AM

Thanks Boss. I've been using a downloaded version of http://en.cppreference.com/w/ along with VS integrated MSDN and a really old book in the vein of "C++ for people who like picture books".

With stringstream my concern is the signedness of the members. It's more or less what I want except for that one issue. The first thing I tried was just digging into the STL headers and trying to find out how to make a stringstream based on unsigned int, but it's buried in such an long and inheritance chain that I gave up after my third "flub<char>diggle::bigarb&". When I get home I'll mess with it a bit and see if it mangles my output.

Ah, so ostream is a parent of both. That should work nicely. Thanks.

As for the book, it looks like what I'm after, but sheesh - \$50.

Guess I'll look into it next time I get paid or see if I can get a second-hand copy somewhere. Thanks again.

Edit - Now that I'm looking at cppreference it would appear that my offline version is criminally out-of-date.

Edited by Khatharr, 21 December 2012 - 04:41 AM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #4SiCrane  Moderators

11530
Like
0Likes
Like

Posted 21 December 2012 - 09:38 AM

Like a lot of the standard library, std::stringstream is actually a template class. It's a typedef for std::basic_stringstream<char>. You could try using a std::basic_stringstream<unsigned char> instead. However, chances are pretty good that won't be necessary and using a regular std::stringstream will just work.

### #5Khatharr  Members

7732
Like
0Likes
Like

Posted 21 December 2012 - 10:05 AM

Reviewing the current version of cppref made everything much more clear.

You're saying that feeding unsigned chars into char based streams won't cause trouble? If the incoming value is greater than 128 won't it get clipped or rotated by the signed type?

Edited by Khatharr, 21 December 2012 - 10:06 AM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #6SiCrane  Moderators

11530
Like
0Likes
Like

Posted 21 December 2012 - 10:22 AM

Well first, char isn't necessarily signed or unsigned. It can be either depending on the compiler. There are three char types: signed char, unsigned char and plain char, with plain char being a separate type but otherwise identical to one of the other two. Most compilers I know of allow you to specify if char is signed or unsigned. For example gcc has the -funsigned-char and -fsigned-char options and MSVC has the /J switch. If you're feeling very paranoid about dumping unsigned chars into a regular char container, you can probably use the set char to unsigned switch for the compiler you are using.

That said, when you dump binary data into a stream, it really doesn't care very much about signed/unsigned. It just sees one byte units of storage. In practice using regular char streams for serialization just works. If you aren't confident, the write some unit tests for the cases that seem like it might cause troubles.

### #7Khatharr  Members

7732
Like
0Likes
Like

Posted 21 December 2012 - 02:17 PM

I'm confident that it will work, or can be made to work when I'm compiling locally. I'm just wondering about what it may get up to on other compilers or if I post the source for other people to compile, since I'm aiming for ease of portability. Is there a standard in terms of how STL will behave in this case? It's really no trouble to use a basic_stringstream<unsigned char> if it guarantees behavior. I'm just curious, since you mentioned it. Thanks again for the advice, btw.

Edited by Khatharr, 21 December 2012 - 02:18 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #8SiCrane  Moderators

11530
Like
0Likes
Like

Posted 21 December 2012 - 05:00 PM

You're going to have to say what case(s) you're looking at before anyone would be able to say what guarantees the standard gives you.

### #9Khatharr  Members

7732
Like
0Likes
Like

Posted 21 December 2012 - 05:57 PM

unsigned char uc = 0xFF;

std::stringstream str;
str.put(uc);
uc = str.get();

//what is uc?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #10SiCrane  Moderators

11530
Like
0Likes
Like

Posted 21 December 2012 - 06:50 PM

There are two possibilities here: char is signed or char is unsigned. If the char is unsigned, then everything is guaranteed to work without a hitch. If char is signed then what happens depends on the platform. The standard is written such that machines that use two's complement representation for signed negative numbers shouldn't change the binary representation when casting from signed to unsigned and back. However, if you've got a machine using sign magnitude for negatives, then there's no telling what value will come out.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.