CS31-12

CS31-12 - CS 31 Introduction to Computer Science I Topic 12...

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 12-1 CS 31 : Introduction to Computer Science I Topic 12 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 9/2009 John A. Rohr All Rights JAR 12-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 9/2009 John A. Rohr All Rights JAR 12-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 9/2009 John A. Rohr All Rights JAR 12-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 9/2009 John A. Rohr All Rights JAR 12-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 9/2009 John A. Rohr All Rights JAR 12-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
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 12-7 File Input and Output File Status Functions good(): No errors or End-Of-File eof(): End-Of-File encountered fail(): Previous operation failed bad(): Input stream error occurred
Background image of page 7

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 9/2009 John A. Rohr All Rights JAR 12-8 File Input and Output File Status Determination Use file variable after open e.g. if(!fv) File not opened succesfully Use good() after each read e.g. if(fv.good()) Data OK for processing Use eof() to check for End-Of-File separately from other conditions e.g. if(fv.eof()) End-Of-File encountered
Background image of page 8
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 12-9 File Input and Output Open and Close Named files must be opened and closed Open sets up the file for input or output Close terminates the file processing Open is required to initialize the file use Close is optional and done automatically if not done explicitly in the program
Background image of page 9

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 9/2009 John A. Rohr All Rights
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 41

CS31-12 - CS 31 Introduction to Computer Science I Topic 12...

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

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