CS31-5 - CS 31: Introduction to Computer Science I Topic 5...

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 5: File Input and Output 1/2010 John A. Rohr All Rights Reserved JAR 5-1 CS 31 : Introduction to Computer Science I Topic 5 File Input and Output
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 5: File Input and Output 1/2010 John A. Rohr All Rights Reserved JAR 5-2 File Input and Output Alternatives to cin and cout cin is used for input from the keyboard cout is used for output to the screen cerr can also be used for screen output These files can sometimes be redirected Input can also come from named files Output can also be written to named files
Background image of page 2
CS 31: Introduction To CS I Topic 5: File Input and Output 1/2010 John A. Rohr All Rights Reserved JAR 5-3 File Input and Output Additional I/O Classes The iostream class provides I/O basics The ifstream class provides file input The ofstream class provides file output Both classes use the “std” prefix #include <fstream> includes both classes using namespace std; provides the prefix
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 5: File Input and Output 1/2010 John A. Rohr All Rights Reserved JAR 5-4 File Input and Output File Declaration Files must be declared like other variables A file object must be declared for File I/O For input files the class is ifstream Example: ifstream infile; For output files the class is ofstream Example: ofstream outfile;
Background image of page 4
CS 31: Introduction To CS I Topic 5: File Input and Output 1/2010 John A. Rohr All Rights Reserved JAR 5-5 File Input and Output File Names A file name can be a simple name or a complete path If a simple name is used the file will be in the same folder as the source code (.cpp) If a full path name is used, forward slashes are used to separate the components
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 5: File Input and Output 1/2010 John A. Rohr All Rights Reserved JAR 5-6 File Input and Output File Status Three separate conditions fail: Operation failure bad: Stream failure eof: End-of-file encountered Good condition: Absence of any failures
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

CS31-5 - CS 31: Introduction to Computer Science I Topic 5...

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

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