# What's the difference between a stream and a queue?

What's the difference between a stream and a queue? They both have the concept of an ordered set of elements, but tend to have different implementations and a different vocabulary of 'insert'/'extract' (streams) vs. 'enqueue'/'dequeue' (queue). Are these interchangable? Do they suggest different concepts or patterns? If so, what are the differences?

Concrete example of 'stream insertion': http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

Potentially useful conceptual pieces?

• Can you give us some concrete examples of streams in real programming languages and/or libraries? The only "streams" I can think of are the C and C++ I/O abstraction which don't have "insert/extract" but rather fread,fwrite and fseek which makes them more like weird extensible arrays than like queues. – Wandering Logic May 23 '13 at 22:59
• @WanderingLogic Thanks for writing--that's exactly the abstraction I'm referring to. If you look in the docs, I believe the standard '<<' and '>>' are called the 'stream insertion' and 'stream extraction' operators. cplusplus.com/reference/ostream/ostream/operator%3C%3C – elliot42 May 23 '13 at 23:01

A queue is an abstract data type with two operations: enqueue() and dequeue() (or sometimes push() and pop()) that have first-in-first-out semantics.

A C stream is hard (for me) to describe abstractly. My best cut at it is that it is an abstraction representing a buffered file (where "file" is understood in the Unix sense of the word as something that is usually (but not always) a sequence of bytes and a current file position.)

At it's most general a stream is like a sequence of bytes numbered from 0, that can be arbitrarily extended at the end, but that has some additional state (the current position). There are some basic operations (fread() and fwrite()) that read or write an arbitrary number of bytes starting at the current position, and then reset the current position to point just after the most recently read or written byte. The current position can also be changed with fseek() and queried with ftell().

Now here's where it gets "icky." At the time a stream is opened a variety of factors determine which of the above operations are enabled or disabled. If you open your stream only for read then calling fwrite() will cause an error. If you open your stream only for write then calling fread() will cause an error. If your stream is actually a pipe or a socket, or is opened in mode append then you are not allowed to fseek(). You are only allowed to read or write from the current file position. stdin, stdout, and stderr are pipes so you can't fseek() them. You can't fwrite() stdin and you can't fread() stdout or stderr.

C++ fixed most of that mess by giving us a hierarchy of types (for example: http://www.cplusplus.com/reference/istream/basic_iostream/), so that we can do error recovery with RAII and can also tell whether the file is readable or writable by looking at the type (although I don't know offhand whether they fixed the seeking mess.)

But then there's the whole other question of formatted output. C has fprintf() which formats some stuff and puts some bytes into a writable stream (starting at the current file position). C++ replaced fprintf() with the (IMHO) really-much-too-cute operator<<(). operator<<() has the advantage of being strongly typed, but the extreme disadvantage of requiring the use of manipulators. So operator<<() puts some bytes to the output stream starting at the current file position, but formatted based on a whole bunch of state that is mutated by manipulators. So a C++ stream actually has a bunch of extra state that you need to know about. (Whether integers are going to get formatted hex or decimal from now on, how much precision floats are printed with, etc.)

So: a queue inserts and removes individual items of a well defined type from its front or its back. A stream is random access extendable array of bytes where you can read and write arbitrary numbers of bytes from the current file position (except when it isn't random access and except when you either can't read or can't write).