 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 File I/O.

Slides:



Advertisements
Similar presentations
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Advertisements

Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  I/O Streams  File I/O  Character.
C/C++ Basics (VII) Streams and File I/O Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter Savitch, “Problem.
Copyright © 2002 Pearson Education, Inc. Slide 1.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Chapter 5: Loops and Files.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Program Input and the Software Design Process ROBERT REAVES.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Learning Objectives I/O Streams – File I/O – Character I/O Tools for Stream I/O – File names as input – Formatting output,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
Loops and Files. 5.1 The Increment and Decrement Operators.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Copyright © 2002 Pearson Education, Inc. Slide 1.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
I/O Streams as an Introduction to Objects and Classes
Chapter 14: Sequential Access Files
Chapter 1.2 Introduction to C++ Programming
ifstreams and ofstreams
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
Chapter 2 part #3 C++ Input / Output
File I/O.
Standard Input/Output Streams
Standard Input/Output Streams
Chapter 6 I/O Streams as an Introduction to Objects and Classes 1.
File I/O.
Introduction to C++ Programming
Today’s Lecture I/O Streams Tools for File I/O
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Reading from and Writing to Files
Chapter 2 part #3 C++ Input / Output
ifstreams and ofstreams
Chapter 1 c++ structure C++ Input / Output
Lecture 3 More on Flow Control, More on Functions,
Reading from and Writing to Files
Presentation transcript:

 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 File I/O

Business  Homework 4 – pass it up  Drop Day – 5pm on Monday, Oct 29 th  Project 4 – Wed, Nov 7 th  Exam 2 – Friday, Nov 9 th 2

 File I/O 3

What is a Stream?  A flow of characters  Input stream - Flow into program  Can come from keyboard  Can come from file  Output stream - Flow out of program  Can go to screen  Can go to file 4

Streams Usage  We’ve used streams already  cin  Input stream object connected to keyboard  cout  Output stream object connected to screen  Streams can also connect to files on disk! 5

File Basics  For this class: text files only  Reading from file  Program takes input data from file  Writing to file  Program sends output data to file  Files are processed from beginning to end  Other methods available (i.e. random access)  We’ll discuss this simple text file access here 6

File I/O Libraries  To allow both file input and output in your program: #include using namespace std;  Library provides two classes:  ifstream – used for input  ofstream – used for output 7

Declaring Streams  Streams must be declared like any other variable: ifstream myInStream; ofstream myOutStream; 8

Connecting Streams  Must connect each file to a stream object myInStream.open("infile.txt"); myOutStream.open("outfile.txt");  Uses member function open to open file  Where is the file located?  Same folder as executable (.exe file)  Anywhere on the hard drive(s)  Can specify complete pathname  "C:\\Users\\khughes\\infile.txt"  Must use \\ because \ is a special character in C++ 9

Streams Usage  Once declared, use normally! (Just like cin and cout…)  Example – Input Stream int oneNumber, anotherNumber; myInStream >> oneNumber >> anotherNumber;  Example – Output Stream myOutStream << "oneNumber = " << oneNumber << " anotherNumber = ” << anotherNumber; 10

File Names  Programs and files  Files have two labels in our programs  External file name (e.g. “infile.txt”)  Sometimes considered “real file name”  Used only once in program (the open() function)  Stream name (e.g. “myInStream”)  Also called “logical file name”  Program uses this name for all file activity 11

Closing Files  Files should be closed when program is finished getting input or sending output  Disconnects stream from file  Example: myInStream.close(); myOutStream.close();  Files automatically close when program ends 12

File Flush  Output is often buffered  Temporarily stored before written to file  Written in “groups”  Occasionally might need to force writing: outStream.flush();  Function flush() works for all output streams  All buffered output is physically written  Closing file automatically calls flush() 13

File I/O Example – Output 14 #include using namespace std; int main() { ofstream output; output.open("U:\\scores.txt"); output << "John" << " " << "T" << " " << "Smith" << " " << 90 << endl; output << "Eric" << " " << "K" << " " << "Jones" << " " << 85 << endl; output.close(); cout << "File written" << endl; return 0; } Contents of File John T Smith 90 Eric K Jones 85

Appending to a File  Standard open operation (for writing) begins with empty file  Even if file exists, contents are erased!  Open for append: ofstream outStream; outStream.open("important.txt", ios::app);  If file doesn’t exist, it is created  If file exists, output is appended to end  2 nd argument is a constant for “input output stream” (ios) 15

Checking File Open Success  File opens could fail for many reasons – ideas?  Input – Input file doesn’t exist  Input – No permission to read file  Output – No write permissions to output file  Output – No space left on disk  Other unexpected errors  Member function fail()  Place call to fail() to check stream operation success inStream.open("stuff.txt"); if (inStream.fail()) { cout << "File open failed.\n"; exit(1); } 16

End of File Check – Using eof()  Use loop to process file until end  Typical approach  Two ways to test for end of file  Member function eof() myInStream >> next; while (!myInStream.eof()) { cout > next; }  Reads each character until file ends  eof() member function returns Boolean 17

End of File Check – Using Read  Second method  read operation returns Boolean value! (myInStream >> next)  Expression returns true if read successful  Returns false if attempt to read beyond end of file  Example: double next, sum = 0; while (myInStream >> next) { sum = sum + next; } cout << "the sum is ” << sum << endl; 18

File I/O Example – Input 19 #include using namespace std; int main() { ifstream input; string nameFirst, nameMiddle, nameLast; int score; // Open file input.open("U:\\scores.txt"); // Read file until end while(!input.eof()) { input >> nameFirst >> nameMiddle >> nameLast >> score; cout << nameFirst << " " << nameMiddle << " " << nameLast << " " << score << endl; } input.close(); return 0; } Console Output John T Smith 90 Eric K Jones 85

File Names as Input  Stream open operation  Argument to open() is C-string type  Can be literal (used so far) or variable char filename[16]; ifstream inStream; cout > filename; inStream.open(filename);  Provides more flexibility – Your program doesn’t have to be hardwired to a single file name 20

File Names as Input  Can also work with C++ strings, but you need to convert it to an null- terminated C-string first 21 string filename; ifstream myInStream; cout << "Enter file name: "; cin >> filename; cout << "Now opening file " << filename << endl; myInStream.open(filename.c_str());

Input Example  Read in a text file containing the names of all US states and territories in alphabetical order  One per line  Print each state on console  One per line 22 Contents of Input File Alabama Alaska Arizona...

23 #include using namespace std; int main() { ifstream input; input.open("states.txt"); if (input.fail()) { cout << "Unable to open input file" << endl; return -1; } char state[32]; while(!input.eof()) { input >> state; cout << state << endl; } input.close(); return 0; } Initial Solution

Initial Solution Output Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio... This is a familiar problem…

getline Example  Read in a text file containing the names of all US states and territories in alphabetical order  One per line  How do we handle states like this?  New Hampshire  New Jersey  New Mexico  New York  North Carolina  North Dakota 25

26 #include using namespace std; int main() { ifstream input; input.open("states.txt"); if (input.fail()) { cout << "Unable to open input file" << endl; return -1; } char state[32]; while(!input.eof()) { input.getline(state, 32, '\n'); cout << state << endl; } input.close(); return 0; } Revised Solution What is happening? getline() will read from the stream “input” and save results to the array “state” until it sees the newline (\n) character, which is discarded. It will read up to 31 characters. The C-string will be null-terminated.

27 #include using namespace std; int main() { ifstream input; input.open("states.txt"); if (input.fail()) { cout << "Unable to open input file" << endl; return -1; } string state; while(!input.eof()) { getline(input, state, '\n'); cout << state << endl; } input.close(); return 0; } Revised Solution using C++ strings What is happening? getline() will read from the stream “input” and save results to the string “state” until it sees the newline (\n) character, which is discarded.

Revised Solution Output  Properly handles multi-word state names  getline is flexible – the delimiter can be any character!  Newline: \n  Tab: \t  Any symbol you want:  Implication: getline can capture multiple lines if the delimiter is NOT the newline character Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio...

>> and getline  If you use both >> and getline on the same input stream, they may get “out of sync” on delimiters  On Lab 6, using cin >> letter followed by getline(array,16) would result in a null C- string being read immediately for the array  This is because >> stops reading at the newline  To handle this, use cin.ignore(100,’\n’) to flush the remaining input 29

 Project 4 – Hangman 30  Project goals – Gain experience with:  File I/O  Strings of characters  Arrays of characters  Arrays of other data types Hang ‘Em High, © United Artists, 1968

Hangman  The goal of the game is to guess a word letter by letter before you run out guesses (and body parts)  Your program needs to  Randomly pick a string of characters from a file  Track which letters have been guessed  Find all letters that match a guess  Recognize when a letter is and is not matched  Update the display 31

Program Operation  Read the entire file once to determine how many lines there are  Tip: Use getline()  Pick a capital at random from the file  Display the gallows and the word  Substitute an underscore ‘_’ for each letter  Tip: for strings, use the.size() function to get the size of the string (i.e., int size = myString.size(); )  Prompt the user for a letter  Determine how many characters in the capital match the letter  If any, need to display them from now on  If none, the user gets a miss 32

ASCII Characters 33

ASCII Character Arithmetic  You can do simple arithmetic to change an ASCII character by adjusting its “number”  You might not need it for this project, but it’s a good thing to know. 34 char letter = 'A'; letter = letter + 3; cout << letter; // output is ‘D’

Program Operation  Questions you need to answer for yourself  How do you know which letters to display, and which to not display?  How do you decide which body parts need to be displayed?  How do you keep track of which letters the user has used?  How do you tell when the user has won?  And how do you test your solutions to these questions? 35

Testcase Development  Why are we requiring test cases?  Common scenario: “Sam” writes 1000 lines of code, compiles and runs it, and finds out the program doesn’t work right  Where should Sam start looking for the error?  Should never have gone this far without testing!  Many people tried to add test cases after writing and testing the full program  This is totally pointless!  Also, a test case isn’t just making a copy of the function and renaming it 36

Testcase Development  Write your twin functions as you are developing the rest of the program  As you write the function, think about how you can test what it does 37

Questions? ? ? ? 38