Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.

Similar presentations


Presentation on theme: " Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output."— Presentation transcript:

1  Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output to files  Reading: 5.5, 8.1-8.3  Exercises: p. 205 #13, 20  New files/handouts: FileAverage.cpp, InData.txt, CheckFile.h

2  Wednesday, 10/16/02, Slide #2 Library Library  The library provides objects and functions that let us do input and output from/to the computer keyboard/monitor  Objects/ types:  cin -- the keyboard input stream, type istream  cout -- the monitor output stream, type ostream  cerr -- the monitor error output stream, type ostream  Functions/operators:  >> extraction operator  << insertion operator  Other I/O functions such as get(), put(), peek(), etc.

3  Wednesday, 10/16/02, Slide #3 Library Library  The library gives us classes that permit us to declare file types, and do file operations.  Our programs can then get input data from a file and send output data to a file.  Types provided:  ifstream -- a type for declaring input files  ofstream -- a type for declaring output files  Functions/operators:  >> input file extraction operator  << output file insertion operator  open() and close(): functions to begin/end associating a file object with an actual file name

4  Wednesday, 10/16/02, Slide #4 Using files: See FileAverage.cpp  1. Declare a file object:  ifstream fin; //name can be anything you want  ofstream fout; //but fin and fout are very standard  2. “Open” the file:  fin.open(“C:\\CS106\\InData.txt”); //Note double quotes  fout.open(“C:\\CS106\\OutData.txt”); //and double backslashes  3. Use the file:  fin >> num; //where num is, say, an int object  fout << num;  4. Close the file:  fin.close();  fout.close();

5  Wednesday, 10/16/02, Slide #5 More on using files  Program treats input file just like keyboard, processing characters in order they appear  Input file data must be separated by spaces or line feeds (not commas, dashes, etc.)  Sometimes hard to get the pathname & filename right -- program tries to input from empty file  Function CheckFile() (in CheckFile.h) checks an input file, and prints error message if opened file was not found or empty.  When you open and begin outputting to a file, anything that was previously in the file is erased!

6  Wednesday, 10/16/02, Slide #6 Getting file input in loops  When we use the extraction operator, it returns a true/false value that depends on whether it successfully/unsuccessfully extracted a value  We can use this value as a loop condition while (fin >> Value) { cout << Value << endl; ++ValuesProcessed; ValueSum += Value; }


Download ppt " Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output."

Similar presentations


Ads by Google