streams

Pages: 12
Can someone explain to me what exactly is the difference between buffers and streams?
Last edited on
http://en.wikipedia.org/wiki/Stream_%28computing%29

Wikipedia wrote:
A stream is a sequence of data elements made available over time. A stream can be thought of as a conveyor belt that allows items to be processed one at a time rather than in large batches.
i have read that but i still don't understand it.
Which part?
With a buffer, you have access to the entire buffer at once. With a stream, you have no idea if the source or destination of the stream even exist. You could just be reading random data from an input stream or writing data to an output stream that ignores your data. Streams abstract the underlying source/target.
> the difference between buffers and streams?

The stream buffer classes represent the abstraction of a connection to an external device. The main task of the stream buffer is transport of characters to and from this external device, and buffering of these characters in an internal buffer.
...
Stream buffers are used by streams for actual transport of characters to and from a device, whereas the streams themselves are responsible for parsing and formatting the text input and output.

- Angelika Langer and Klaus Kreft in 'Standard C++ IOStreams and Locales'


Book excerpt: http://www.angelikalanger.com/IOStreams/Excerpt/excerpt.htm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <memory>

int main()
{
    std::streambuf* stdout_buffer = std::cout.rdbuf() ; // the buffer that sends output to stdout
    std::filebuf fbuf ;
    fbuf.open( "/tmp/half_of_jabberwocky", std::ios::out ) ; // the buffer that sends output to a file
    std::streambuf* file_buffer = std::addressof(fbuf) ;
    
    const char* const jabberwocky[] = 
    {
        "’Twas brillig, and the slithy toves",
        "Did gyre and gimble in the wabe:",
        "All mimsy were the borogoves,",
        "And the mome raths outgrabe.",
        
        "“Beware the Jabberwock, my son!",
        "The jaws that bite, the claws that catch!",
        "Beware the Jubjub bird, and shun",
        "The frumious Bandersnatch!”",
        
        "He took his vorpal sword in hand;",
        "Long time the manxome foe he sought—",
        "So rested he by the Tumtum tree",
        "And stood awhile in thought."        
    };

    for( const char* line : jabberwocky )
    {
        static int n = 0 ;
       
        // std::cout forwards the characters to be written to the buffer
        // the buffer periodically flushes the characters to the output device
        // (in this case, at the end of each line, because std::cout asks it to flush the buffer.)
        std::cout << ++n << ". " << line << '\n' ;

        // switch buffers after each line. 
        // if the buffer is stdout_buffer, output will be written to stdout
        // if it is file_buffer, output will be written to the file 
        std::cout.rdbuf( n%2 == 1 ? file_buffer : stdout_buffer ) ; // replace buffer
    }
    
    std::cout.rdbuf( stdout_buffer ) ; // restore original buffer
}

echo -e 'stdout\n------'
clang++ -std=c++11 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && ./a.out
echo -e '\nfile /tmp/half_of_jabberwocky\n-------------------------------'
cat /tmp/half_of_jabberwocky
stdout
------
1. ’Twas brillig, and the slithy toves
3. All mimsy were the borogoves,
5. “Beware the Jabberwock, my son!
7. Beware the Jubjub bird, and shun
9. He took his vorpal sword in hand;
11. So rested he by the Tumtum tree

file /tmp/half_of_jabberwocky
-------------------------------
2. Did gyre and gimble in the wabe:
4. And the mome raths outgrabe.
6. The jaws that bite, the claws that catch!
8. The frumious Bandersnatch!”
10. Long time the manxome foe he sought—
12. And stood awhile in thought.

http://coliru.stacked-crooked.com/a/bbeaf11fe8a29956
Last edited on
I don't understand most of that code.
Input stream = Keyboard -> Program.
Output stream = Program -> Device.
Stringstream = ? -> ?
Input Stream = Input Source -> Program
Output Stream = Program -> Output Destination

A std::istringstream takes input from a string.
A std::ostringstream sends output to a string.
Last edited on
So if you use istringstream there must be a cin or getlin(cin, str) ?
and ostringstream is just giving a string a sentence or word to print?
I don't know what you mean. std::istringstream has no relation to std::cin other than the fact that they are both input streams.
Last edited on
Then i dont know what you mean with
A std::istringstream takes input from a string.
and
A std::ostringstream sends output to a string.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string numbers = "100 40 32";
int x, y, z;

{
    std::istringstream iss (numbers); //take input from the string
    iss >> x; //x == 100
    iss >> y; //y == 40
    iss >> z; //z == 32
}

{
    std::ostringstream oss; //send output to a string
    oss << z << " ";
    oss << y << " ";
    oss << x;
    std::string result = oss.str(); //result == "32 40 100"
}
If only there was some web site where you could look up tutorials and reference information about stringstreams!
Should,
1
2
3
4
5
6
7
{
    std::ostringstream oss; //send output to a string
    oss << z << " ";
    oss << y << " ";
    oss << x;
    std::string result = oss.str(); //result == "32 40 100"
}


not print 32 40 100? it didn't print anything for me.

i just did cout << oss.str(); and it printed it but does istringstream not just do the same as ostringstream or do istringstream just copy the string and ostringstream puts the values to the variables x,y,z? and how come one string can be split into three variables? is it after space or something?
Last edited on
The questions you're asking make me think you've never written programs using std::cin before. You should do some more C++ tutorials and examples to further your understanding on this topic first.
i have written programs using
1
2
3
int myAge;
cin >> myAge;
cout << myAge;
mostly like that. never used string with cin or stringstreams.
Last edited on
does istringstream not just do the same as ostringstream

Do cin and cout do the same thing?

cin is an istream. cout is an ostream. Why would you expect istringstream (which is an istream), and ostringsteam (which is an ostream) to do the same thing?

I suggest you read the following reference pages:
http://www.cplusplus.com/reference/istream/istream/
http://www.cplusplus.com/reference/ostream/ostream/
http://www.cplusplus.com/reference/sstream/istringstream/
http://www.cplusplus.com/reference/sstream/ostringstream/


but ostringstream doesnt cout it directly?
Last edited on
Only std::cout sends output to the console. Only a std::ofstream sends output to a file. Only a std::ostringstream sends output to a string.
i know what a console and a file is and what is string compared to them?
Pages: 12