First off, all streams are derived from basic_streambuf, the lowest-level class for I/O. Everything gets derived quite a few times, so you'll most likely won't ever see that. Anyway, I'd like to start out by saying that one of the main advantages to using streams is that since all streams derive from istream and/or ostream, you can read and write any stream, no matter whether it affects the console display, a file, a network connection, etc. This means that you can have a function take a stream input and will work the same for both keyboard input and file input, with no modification to the code
Anyway, let's get crackin'. The three file stream types are:
- std::fstream
- std::ifstream
- std::ofstream
std::ifstream - Same as above, but only allows input (so you can only read the file)
std::ofstream - Same as first, but only allows output (only writing)
Before we really begin, let's learn some basic formatting. These member functions (the C++ name for these is 'string manipulators') are from ios (or ios_base, I forget), so any stream class will support these.
- left - Left-justifies text
- right - Right-justifies text
- width(streamsize s) - Sets the width of the fielf (for right-justification) ***
- fill(char c) - The character to fill blank spaces with ***
- oct - Use the octal (radix of number system to display numbers
- dec - Use the decimal (base-10) number system (default)
- hex - Use the hexadecimal (radix 16) number system
- flush - Flushes the buffer (actually writes the data)
- endl - Adds a newline to the stream and flushes it
You can use those in two main ways (there is another but I don't want to go over it):
-Call them as member functions
-Call them in a stream expression
Here's an example using the good ol' iostream class cout (all classes from the ios family work):
Code: Select all
//Method 1
#include <iostream>
int main()
{
std::cout.width(15);
std::cout.fill('.');
std::cout << 256;
}
//output:
//............256
Code: Select all
//Method 2
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setw(15) << std::setfill('.') << 256;
}
//output: same as above
Now here's an example showing use of everything I just said:
Code: Select all
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setw(50) << std::setfill('#');
std::cout << std::right << "Hello!" << std::endl;
std::cout << std::left << std::oct << 40 << " (oct), " << std::dec << 40 << " (dec), " << std::hex << 40 << " (hex)." << std::endl;
}
//output:
//############################################Hello!
//50 (oct), 40 (dec), 28 (hex).
Now, I'll show you how to make your very own manipulators!
Say you do a lot of:
Code: Select all
std::clog << "\t***ERROR***" << std::endl;
Code: Select all
std::ostream &err(std::ostream &s)
{
s << "\t***ERROR***";
return s;
}
Here's the code:
Code: Select all
#include <iostream>
#include <iomanip>
std::ostream &err(std::ostream &s)
{
s << "\t***ERROR***";
return s;
}
int main()
{
std::cout << "Hi... " << err << std::endl;
std::cin.get();
}
//output:
//Hi... ***ERROR***
- read(char *d, streamsize s) - Reads s characters and places them in d.
- write(const char *d, streamsize s) - Writes s characters of d
- get() - Gets a single character (return value is the character)
- unget() - Puts the character back in the buffer that you just read
- peek() - Peeks ahead one character in the buffer (doesn't move the pointer to the current location)
- put() - Puts a character (duh)
- seekg(streamoff o, ios_base::seekdir d) - Seeks the input pointer to offset o from ios::beg, ios::cur, or ios::end (the beginning, current position, or end of a stream)
- seekp(streamoff o, ios_base::seekdir d) - Same as above, but for the output
- tellg() - Gives current location in input stream
- tellp() - Same, for output
- clear() - Clears state of stream object (i.e. clears any errors like eof() below)
- eof() - Returns a bool. "Has the End-of-File been reached?"
- good() - Returns a bool. "Is this stream good?"
- fail() - Returns a bool. "Has this stream failed?"
- bad() - Returns a bool. "Is this stream bad?"
- getline(char *s, streamsize n, char d) - Reads a line. s is the string to put the data in. n is the size of the string s. d is an optional parameter (well actually getline is overloaded but w/e) specifying the 'line-break' character (you could say it's '