{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CP1_8_File_IO_1112

CP1_8_File_IO_1112 - File File I/O Communicating...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
File I/O
Image of page 1

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

View Full Document Right Arrow Icon
Communicating with our programs So far all the data input to our programs has come So far, all the data input to our programs has come from the keyboard, and all the output has been to the display using cin and cout, the standard input and t t t output streams. This is inconvenient except for tiny amounts of temporary data that we don't need to store for later use. It would be better to have the ability to store data in more permanent data files that can be read and/or written by our programs 2 CSIS1117B Computer Programming 1 2011-2012
Image of page 2
Using other sources and destinations We visualized cin and cout as streams of data connected to the keyboard and display to the keyboard and display. We used the extraction operator >> to take data from the input stream and the insertion operator << to put data into the output stream. But we can also use streams to move data into and out of other sources and destinations by connecting them to those locations. For example we can connect an input stream to a file For example, we can connect an input stream to a file to read data from that file 3 CSIS1117B Computer Programming 1 2011-2012
Image of page 3

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

View Full Document Right Arrow Icon
Streams In C++, a stream is a sequence of data which flows into or out of an executing program A i t t i fl f An input stream is a flow from a source into our program. We extract data from the stream one by one one by one. An output stream is a flow out of our program to a destination of our program to a destination. We insert data into the stream one by one. 4 CSIS1117B Computer Programming 1 2011-2012
Image of page 4
Using streams In <iostream> cin is already declared for our use as an <iostream> , cin input stream and cout as an output stream. We must have the directive #include <iostream> in our program t th to use them. If we wish to use other streams, however, we must explicitly declare variables of the appropriate type in o p og am and incl de the ele ant lib a our program and include the relevant library. 5 CSIS1117B Computer Programming 1 2011-2012
Image of page 5

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

View Full Document Right Arrow Icon