new-file-io

new-file-io - File Input and Output in C+ How much Data?...

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

View Full Document Right Arrow Icon
File Input and Output in C++
Background image of page 1

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

View Full DocumentRight Arrow Icon
How much Data? For very small amounts of data, just code it into the program “x = 5;” For small amounts of data, ask the user to type it in “cin >> x >> y >> z;” For medium and large amounts of data, what do you do? An EXTERNAL FILE They are stored on secondary storage, like a hard drive or memory stick or floppy They can have more data than RAM can hold!
Background image of page 2
Why external files? Handles large amounts of data Storage of data independent of the program, so program doesn’t have to be changed if data changes Easier to edit the data Can use the same data as input for different programs Can save output data for use later Can use output of one program as input of another
Background image of page 3

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

View Full DocumentRight Arrow Icon
Keyboard and Screen I/O #include <iostream> cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data output data 4 0
Background image of page 4
File I/O #include <fstream> stream variable (of type ifstream) Input.txt Report.txt executing program input data output data 5 0 stream variable (of type ofstream)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Using File I/O Include Library #include <fstream> Declare your file streams - choose valid, meaningful identifiers for them and the types ifstream and ofstream Open the files (associate disk files with file streams) Use your file stream identifiers in your I/O statements (using >> and << , manipulators, get, ignore, etc…) Close the files when done with them 6
Background image of page 6
File I/O Statements #include <fstream> // Declare File Streams ifstream   myInfile;  // for input ofstream  myOutfile;   // for output myInfile.open(“myIn.dat”);   // Open input file myOutfile.open(“myOut.dat”);  // Open output file // Use file streams for input and output // Close files myInfile.close(); myOutfile.close(); 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
File Streams are Variables cin and cout are automatically created You must declare your own stream variables to use file I/O For Input…
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/13/2011 for the course CS 115 taught by Professor Staff during the Fall '08 term at Kentucky.

Page1 / 26

new-file-io - File Input and Output in C+ How much Data?...

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

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