streams - what is sense of this code?

I found one article which is not in English but there are some parts in the code which are very confusing. The code was not explained too much good but I see no sense of

http://paste.ofcode.org/Mr6p4RUNWyZKggSZwifYiN

ostrstream proud(buffer,15,ios::ate | ios::app);

Which says it cannot add data to buffer.

I commented the cin.get thing, because I see non-sense to use it here.

I see no point of it. Do you have idea why author sets the actual position of pointer to the end of file/buffer and then tries to add data? It is logical that it will not be possible to add data, dont you think? The if block will never be true. Non sense example huh?

What is wrong with this code? I would like to change it to work like this:
I would like to append data on the begin of the buffer and then, if the string is too long, so it will print error, but if it is not too long, so it will print success.
Last edited on
You are exactly right -- the buffer is too small. The problem is that the old strstream class has no idea that it is too small -- it believes you when you tell it that the buffer is bigger than it is.

Since you are using Microsoft's compiler, there is actually no need for you to use the old strstream classes. Use the modern stringstream instead. Then you never need to worry that the buffer is too small.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
   ostringstream proud;

   proud << "My text" << endl;
   cout << proud.str();

   cin.get();
   return 0;
}

Hope this helps.
Oh yes, this looks much better. But how you do it to to add some data from keyboard?
Last edited on
Well, an ostream is an output stream -- by definition it does not accept input.

I was a little confused by your original code there. What exactly is it you are trying to do?
I am not sure what the original code should do. I think it should illustrate the limit of the buffer. So if you get "hello" in char[10] you are OK, but if you try "Hello World" so you fail because you are out of the limit.
Hmm, well, you might want to just ignore it. The old streams stuff isn't used any more, in part because of weirdness like this.
Well, thanks.
Topic archived. No new replies allowed.