Notes

You want to do something new with IoT, RFID ?. Drop me a message :).

Monday, January 17, 2011

C/C++ Input Output Stream

Le Trung Thang 2011

I/O Stream hierarchical in C++ standard

A Stream need to have the features follow:
-  A Stream always associates with a data source or a data sink. Input Stream always associates with a data source (producer) which can supply the stream of bytes to the Input Stream. Oppositely,  Output Stream always associates with a data sink (consumer) which can consume the stream of bytes putted out from the output stream.

- Stream must accommodate a way to be able to push data out or get data in, in order. Data may be the byte streams (Raw data ) or the char streams (Formatted data). For example, in C standard, the methods to push data to consumer are putchar or write. The methods to get data from producer are getchar or read. These methods are low-level methods which manipulate directly the device.

- In high level language programmings, as C++/JAVA, a Stream also can provide the methods to format the data before the data is pushed in to it's consumer or after data is gotten out from it's producer.
In C, the concreting of Stream is a pointer to the FILE object. In C++ is ios object, of  course, C++ is also support FILE type, too and  in JAVA, that is InputStream object and OutputStream object.

I/O Stream hierarchical in Java
JAVA example code:
    
String message = " hello world" ; 
 ByteArrayOutputStream bos = new ByteArrayOutputStream(); //create a data sink (consumer)
ObjectOutputStream oStream = new ObjectOutputStream(bos); //create output stream associate with data sink above
oStream.writeObject(message); //format "message" Object and then push it to "oStream" Stream
 
Reference: http://cboard.cprogramming.com/c-programming/134041-file-object-c-cplusplus.html

(Thang Le is mine :) )

=======================More Reference=============================
( from: http://en.wikibooks.org/wiki/C++_Programming/Code/IO )


I/O
Also commonly referenced as the C++ I/O of the C++ standard since the standard also includes the C Standard library and its I/O implementation, as seen before in the Standard C I/O Section.
Input and output are essential for any computer software, as these are the only means by which the program can communicate with the user. The simplest form of input/output is pure textual, i.e. the application displays in console form, using simple ASCII characters to prompt the user for inputs, which are supplied using the keyboard.

There are many ways for a program to gain input and output, including
  • File i/o, that is, reading and writing to files
  • Console i/o, reading and writing to a console window, such as a terminal in UNIX-based operating systems or a DOS prompt in Windows.
  • Network i/o, reading and writing from a network device
  • String i/o, reading and writing treating a string as if it were the input or output device
While these may seem unrelated, they work very similarly. In fact, operating systems that follow the POSIX specification deal with files, devices, network sockets, consoles, and many other things all with one type of handle, a file descriptor. However, low-level interfaces provided by the operating system tend to be difficult to use, so C++, like other languages, provide an abstraction to make programming easier. This abstraction is the stream.

Streams
A stream is a type of object from which we can take values, or to which we can pass values. This is done transparently in terms of the underlying code that demonstrates the use of the std::cout stream, known as the standard output stream.

// 'Hello World!' program 
#include "iostream"
int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
} 

Almost all input and output one ever does can be modeled very effectively as a stream. Having one common model means that one only has to learn it once. If you understand streams, you know the basics of how to output to files, the screen, sockets, pipes, and anything else that may come up. A stream is an object that allows one to push data in or out of a medium, in order. Usually a stream can only output or can only input. It is possible to have a stream that does both, but this is rare. One can think of a stream as a car driving along a one-way street of information. An output stream can insert data and move on. It (usually) cannot go back and adjust something it has already written. Similarly, an input stream can read the next bit of data and then wait for the one that comes after it. It does not skip data or rewind and see what it had read 5 minutes ago. The semantics of what a stream's read and write operations do depend on the type of stream. In the case of a file, an input file stream reads the file's contents in order without rewinding, and an output file stream writes to the file in order. For a console stream, output means displaying text, and input means getting input from the user via the console. If the user has not inputted anything, then the program blocks, or waits, for the user to enter in something.

Buffers

Most stream objects, including 'cout' and 'cin', have an area in memory where the information they are transferring sits until it is asked for. This is called a 'buffer'. Understanding the function of buffers is essential to mastering streams and their use.

 












---------

No comments: