How to create my own stream buffer

I have spent all the day but I'm unable to create a stream buffer and 'play' with it.
I want to have a i-o stream based on a known size buffer.
Later I want to put and get data, like if it was a file, but it isn't a file.
Please, can anybody help me?

Where to start ? Cplusplus reference is a good place but I dont see how to begin...
Thanks
I think you just mean that you want to overload the stream operators. If that's the case, then you would start with operator overloading.

EDIT: HEre is a good overview -> http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/extraction.html

Last edited on
Any reason a Vector wouldn't work for you? I can understand if you want to do it this way for educational purposes though.
I want to put and get data into a buffer, can I do it with vectors?
My 'data' can have any kind of data types...
Thanks
The vector would be a dynamically sized array that you could asign to any one data type per vector.
Hello computergeek.
I wish you help me.
My questions about this theme (buffers, streams, etc) are related around this problem:
I want to 'play' with data, fill my char [] buffer and then write/read form disk.

How can I use this char[] buffer like a file ? That is to say, I want to use seek, put,get,>>,<<, etc.

If I have this char[] buffer, is there a way to use as 'stream' ? How?
I remember that with Java was easy.

Please, helppppp.... Thanks
Last edited on
You seem to be over complicating things. The beginning of an array is always '0', as for the end of the array I suggest setting it's size with an integer you can reference later. Something like this:
1
2
3
4
5
const int X = 10; /*Variable to set the size of the array*/
char array[X];
/* that way later on you can do cool stuff without thinking about the size of your array */
for(int i = 0; i <= X; ++i)
{/*Do something with array[i] */}

See if we had set the size of array to '10' by writing char array[10]; we would have to change it in every place we go threw the array. Now we change 'X' and everything knows the size of 'array[]'.

EDIT: If you used a vector you wouldn't even have to set the size of the array. http://www.cplusplus.com/reference/stl/vector/
Last edited on
I dont know If all is because my bad english.... (nobody understand my need)

Imagine I have to store a float, 3 doubles, 1 string , and other 2 floats into a char array.
How can I do this ?
This is because I'm praying to find something similar to a 'memorystream'....
Do you understand me now ?
Thanks for your patience


You may have lost me now... Maybe Union? http://www.cplusplus.com/doc/tutorial/other_data_types/#union
But that is a trickey one to learn.
uff.
I'm going to explain it again.
You know the features of fstream ok?
Well, then I want to have a similar object (a memory stream buffer, if it can be called so) to get and put data, but to-from memory, not to-from disk.

I think the basic iostream can be my object, but I dont know how to create and use it.

My typical work could be:
I write data (any kind of data:1 float, 3 doubles, 1 string, 1 char [], etc ) to my 'buffer'. When it reaches the size I want I save it to a file (or maybe I send to another computer). Then I will read it.
Using Java streams I can do it. Why not with c++
I hope you understand me now.
Thanks








You are looking for a stringstream. That's what we C++ people call our Java MemoryStreams.
But ... can I use floats, doubles, etc to put-get into stringstream?
An thanks for your two answers...
Yes, the same way you do with normal streams.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
  {
  // Demonstrate an inverse variation
  istringstream ss( "9y = 72" );

  double x;
  double k;
  string name;
  string equalsign;

  ss >> x >> name >> equalsign >> k;
  cout << "Given \"" << ss.str() << "\", then " << name << " = " << (k / x) << ".\n";

  return 0;
  }

Hope this helps.
Topic archived. No new replies allowed.