8. file IO

8. file IO - ENGG1002 Computer Programming and Applications...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGG1002 Computer Programming and Applications Prepared by Dr. K.K.Y. Wong
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Streams Recall that C++ uses a convenient abstraction called streams to perform input and output operations in sequential media, e.g., cin is a stream object for taking input from keyboard cout is a stream object for sending output to the screen Other than the standard input and output streams, C++ also provides two classes , namely ifstream and ofstream , for reading and writing data from and to files To use the classes ifstream and ofstream , simply include the header file fstream , i.e., 2 #include <fstream>
Background image of page 2
ifstream An input file stream object can be declared using the class name ifstream Before data can be extracted from the stream, the object has to be connected to a file using the member function open() with the filename as an argument The member function fail() (with no arguments) is used to check if there is any error in opening the file After successfully opening the file, data can be extracted from the stream using the extraction operator >> When the data reading is done, the stream object should be disconnected from the file using the member function close() (with no arguments) 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

8. file IO - ENGG1002 Computer Programming and Applications...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online