CP1_8_File_IO_1112

CP1_8_File_IO_1112 - File File I/O Communicating Communicating with our programs So far all the data input to our programs has come all from the

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

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

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

View Full DocumentRight Arrow Icon
Communicating with our programs o 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 output streams. his is inconvenient except for tiny amounts of 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
Background image of page 2
Using other sources and destinations We visualized cin and cout as streams of data connected the keyboard and isplay to the keyboard and display. We used the extraction operator >> to take data from p 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. or 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Streams In C++, a stream is a sequence of data which flows into or out of an executing program An input stream is a flow from a source into our program. We extract data from the stream ne y one one by one. An output stream is a flow out f 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
Background image of page 4
Using streams n stream in already declared r our use as n In <iostream> , cin is already declared for our use as an input stream and cout as an output stream. We must have the directive #include <iostream> in our program to use them. If we wish to use other streams, however, we must explicitly declare variables of the appropriate type in our program and include the relevant library. 5 CSIS1117B Computer Programming 1 2011-2012
Background image of page 5

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

View Full DocumentRight Arrow Icon
File streams To read data from a file, we use an input-file stream To write data to a file, we use an output-file stream We need to: declare a file stream variable, and connect it to the required file located in some directory The data type for input-file stream variables is ifstream . For output-file stream variables it is ofstream . ifstream and ofstream are defined in the library <fstream> . 6 CSIS1117B Computer Programming 1 2011-2012
Background image of page 6
Using File streams For example, to create and use a file output stream: eclare a ariable f type fstream Declare a variable of type ofstream Connect the stream variable to a file Output data to the stream by using the insertion operator << Disconnect the variable from the file when we are done ofstream and ifstream are classes. We’ve already worked with a class when we used string .
Background image of page 7

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

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

This note was uploaded on 12/15/2011 for the course CSIS 1117B taught by Professor Noneedtoknow during the Winter '11 term at HKU.

Page1 / 27

CP1_8_File_IO_1112 - File File I/O Communicating Communicating with our programs So far all the data input to our programs has come all from the

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

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