input-output-tokens
input-output-tokens
1. Stream IO
1.1 Streams
1.2 C++ IO Headers, Templates and Classes
1.3 Buffered IO
1.4 The <iostream> Header and the Standard Stream Objects: cin, cout, cerr and
clog
1.5 The Stream Insertion << and Stream Extraction >> Operators
1.6 The ostream Class
1.7 The istream class
1.8 Unformatted Input/Output Functions
1.9 States of stream
1.10 Formatting Input/Output via Manipulators in <iomanip> and <iostream>
1.11 The C++ string class Input/Output
2. File Input/Output (Header <fstream>)
2.1 File Output
2.2 File Input
2.3 Example on Simple File IO
2.4 Binary file, read() and write()
2.5 Random Access File
3. String Streams
Template Classes
In order to support various character sets (char and wchar_t in C++98/03; and
char16_t, char32_t introduced in C++11), the stream classes are written as
template classes, which could be instantiated with an actual character type. Most
of the template classes take two type parameters. For example,
template <class charT, class traits = char_traits<charT> >
class basic_istream;
// Examples
cin.ignore(numeric_limits<streamsize>::max()); // Ignore to the end-of-file
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore to the end-of-line
1.8 Unformatted Input/Output Functions
put(), get() and getline()
The ostream's member function put() can be used to put out a char. put() returns
the invoking ostream reference, and thus, can be cascaded. For example,
// ostream class
ostream & put (char c); // put char c to ostream
// Examples
cout.put('A');
cout.put('A').put('p').put('p').put('\n');
cout.put(65);
// istream class
// Single character input
int get ();
// Get a char and return as int. It returns EOF at end-of-file
istream & get (char & c);
// Get a char, store in c and return the invoking istream reference
// C-string input
istream & get (char * cstr, streamsize n, char delim = '\n');
// Get n-1 chars or until delimiter and store in C-string array cstr.
// Append null char to terminate C-string
// Keep the delim char in the input stream.
istream & getline (char * cstr, streamsize n, char delim = '\n');
// Same as get(), but extract and discard delim char from the
// input stream.
// Examples
int inChar;
while ((inChar = cin.get()) != EOF) { // Read till End-of-file
cout.put(inchar);
}
[TODO] Example
read(), write() and gcount()
// istream class
istream & read (char * buf, streamsize n);
// Read n characters from istream and keep in char array buf.
// Unlike get()/getline(), it does not append null char at the end of input.
// It is used for binary input, instead of C-string.
streamsize gcount() const;
// Return the number of character extracted by the last unformatted input
operation
// get(), getline(), ignore() or read().
// ostream class
ostream & write (const char * buf, streamsize n)
// Write n character from char array.
// Example
[TODO]
Other istream functions - peek() and putback()
char peek ();
//returns the next character in the input buffer without extracting it.
// fixed-point formatting
cout << fixed;
cout << "|" << 1234567.89 << "|" << endl; // |1234567.890000|
// default precision is 6, i.e., 6 digits after the decimal point
// scientific formatting
cout << scientific;
cout << "|" << 1234567.89 << "|" << endl; // |1.234568e+006|
// default precision is 6, i.e., 6 digits after the decimal point
// Test precision
cout << fixed << setprecision(2); // sticky
cout << "|" << 123.456789 << "|" << endl; // |123.46|
cout << "|" << 123. << "|" << endl; // |123.00|
void close (); // Closes the file, flush the buffer and disconnect from stream
object
bool is_open (); // Returns true if the file is successfully opened
File Modes
File modes are defined as static public member in ios_base superclass. They can
be referenced from ios_base or its subclasses - we typically use subclass ios. The
available file mode flags are:
1 ios::in - open file for input operation
2 ios::out - open file for output operation
3 ios::app - output appends at the end of the file.
4 ios::trunc - truncate the file and discard old contents.
5 ios::binary - for binary (raw byte) IO operation, instead of character-based.
6 ios::ate - position the file pointer "at the end" for input/output.
You can set multiple flags via bit-or (|) operator, e.g., ios::out | ios::app to append
output at the end of the file.
For output, the default is ios::out | ios::trunc. For input, the default is ios::in.
2.2 File Input
The steps are:
1 Construct an istream object.
2 Connect it to a file (i.e., file open) and set the mode of file operation.
3 Perform output operation via extraction << operator or read(), get(), getline()
functions.
4 Disconnect (close the file) and free the istream object.
#include <fstream>
.......
ifstream fin;
fin.open(filename, mode);
......
fin.close();
// Get contents
cout << sout.str() << endl;
The ostringstream is responsible for dynamic memory allocation and
management.
istringstream
explicit istringstream (ios::openmode mode = ios::in); // default with empty
string
explicit istringstream (const string & buf,
ios::openmode mode = ios::in); // with initial string
For example,
// construct input string stream (buffer) - need <sstream> header
istringstream sin("123 12.34 hello");
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float pi;
cout<<”enter the value of pi”;
cin>>pi; 3.1435245
cout.precision(2);
cout<<”pi=”<<pi; 3.14
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout.fill(‘/’);
cout.width(5);
cout<<”A”;
cout.fill(‘*’);
cout.width(15);
cout<<”B”;
getch();
}
////A**************B