Which of the following is the member function that reads a single character from a file?
The C language did not build the input/output facilities into the language. In other words, there is no keyword like Show
Features
Stream IOStreamsC/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs (just like water and oil flowing through a pipe). In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program. In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program). Streams acts as an intermediaries between the programs and the actual IO devices, in such the way that frees the programmers from handling the actual devices, so as to archive device independent IO operations. C++ provides both the formatted and unformatted IO functions. In formatted or high-level IO, bytes are grouped and converted to types such as To perform input and output, a C++ program:
C++ IO Headers, Templates and ClassesHeadersC++ IO is provided in headers Template ClassesIn order to support various character sets ( template where:
Template Instantiations and typedefAs mention, the typedef basic_ios Specialization Classes for char typeWe shall focus on the specialization classes for
Buffered IO[TODO] The |
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 | #include |
Program Notes:
- Most of the
functions (such as constructors,open()
) supports filename in C-string only. You may need to extract the C-string fromstring
object via thec_str()
member function. - You could use
is_open()
to check if the file is opened successfully. - The
get(char &)
function returns a null pointer (converted tofalse
) when it reaches end-of-file.
Binary file, read() and write()
We need to use read()
and write()
member functions for binary file (file mode of ios::binary
), which read/write raw bytes without interpreting the bytes.
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 | #include |
Random Access File
Random access file is associated with a file pointer, which can be moved directly to any location in the file. Random access is crucial in certain applications such as databases and indexes.
You can position the input pointer via seekg()
and output pointer via seekp()
. Each of them has two versions: absolute and relative positioning.
istream & seekg (streampos pos); istream & seekg (streamoff offset, ios::seekdir way); streampos tellg (); ostream & seekp (streampos pos); ostream & seekp (streamoff offset, ios::seekdir way); streampos tellp ();
Random access file is typically process as binary file, in both input and output modes.
[TODO] Example
String Streams
C++ provides a
header, which uses the same public interface to support IO between a program and string
object (buffer).
The string streams is based on ostringstream
(subclass of ostream
), istringstream
(subclass of istream
) and bi-directional stringstream
(subclass of iostream
).
typedef basic_istringstreamistringstream; typedef basic_ostringstream ostringstream;
Stream input can be used to validate input data; stream output can be used to format the output.
ostringstream
explicit ostringstream (ios::openmode mode = ios::out); explicit ostringstream (const string & buf, ios::openmode mode = ios::out); string str () const; void str (const string & str);
For example,
ostringstream sout;
sout << "apple" << endl;
sout << "orange" << endl;
sout << "banana" << endl;
cout << sout.str() << endl;
The ostringstream
is responsible for dynamic memory allocation and management.
istringstream
explicit istringstream (ios::openmode mode = ios::in); explicit istringstream (const string & buf, ios::openmode mode = ios::in);
For example,
istringstream sin("123 12.34 hello"); int i; double d; string s; sin >> i >> d >> s; cout << i << "," << d << "," << s << endl;
Link to "C++ Language References & Resources"