1 Text File I/O Chapter 6 Pages 262 - 274. 2 File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.

Slides:



Advertisements
Similar presentations
File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
Advertisements

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.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
File streams Chapter , ,
1 DOS vs. UNIX files Ending lines with “\r\n” vs. “\n” Reading an entire line at a time getline() To skip white space or not cin >> ch; vs. ch = cin.get();
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File I/O Supplemental Material. Background In C++, files can be manipulated in the same manner we manipulate streams such as: cout and cin. Therefore,
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.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Program Input and the Software Design Process ROBERT REAVES.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Chapter 8 Data File Basics.
CS Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
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.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
1 Getting Started with C++ Part 2 Linux. 2 Getting Started on Linux Now we will look at Linux. See how to copy files between Windows and Linux Compile.
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
Files To read a file – We must know its name – We must open it (for reading) – Then we can read – Then we must close it That is typically done implicitly.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
1 Huffman Codes Computing an Optimal Code for a Document.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
ifstreams and ofstreams
Lecture 9 Files, Pointers
File I/O.
Standard Input/Output Streams
Interactive I/O Input from keyboard Must prompt user User friendly
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
File Input and Output.
Topics Input and Output Streams More Detailed Error Testing
Standard Input/Output Stream
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
ifstreams and ofstreams
File I/O in C++ II.
Lecture 9 Files Handling
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

1 Text File I/O Chapter 6 Pages

2 File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening a file for output in C. The object holds the state information that is in the FILE struct in C. Call methods of the object to write the file.

Writing a Text File #include int main() { using namespace std; ofstream outfile; cout << "This program writes a short text file "; outfile.open("test.txt"); outfile << "Here is the first line of text\n"; outfile << "Here is the second line\n"; outfile << "Here is the third and final line\n"; outfile.close(); cout << "File test.txt written\n"; cin.get(); // Keep window open; return 0; }

4 Writing a Text File Where is it?

5 Writing a Text File When running from Visual Studio, the default directory is the project directory. Double click on file to open in Notepad.

6 Here is the File in Notepad

7 Get Filename from Keyboard We often want the user to specify the filename. Must be C String (array of char)

Get Filename from Keyboard #include int main() { using namespace std; char filename[500]; ofstream outfile; cout << "Name for output file? "; cin >> filename; outfile.open(filename); outfile << "Here is the first line of text\n"; outfile << "Here is the second line\n"; outfile << "Here is the third and final line\n"; outfile.close(); cout << "File " << filename << " written\n"; cin.get(); // Keep window open; cin.get(); // Need a second get() now. return 0; }

9 Program Running on Windows As before, the output file is in the project directory.

10 Reading a Text File #include Instantiate an ifstream object Call methods of the object to do input.

11 Reading a Text File #include #include // for file I/O #include // for exit() int main() { using namespace std; char filename[500]; ifstream infile; char input_line[500]; cout << "File input example\n"; cout << "Name of file to read: "; cin >> filename; cout << "Opening file " << filename << endl; infile.open(filename); Note that filename is a C String (char array)

12 Reading a Text File if (!infile.is_open()) { cout << "Failed to open file\n"; cin.get(); // Keep window open. cin.get(); return -1;// Error } // Input file is open while (infile.good()) { infile >> input_line; cout << "Next line is: " << input_line << endl; } Always check for success of fopen Read the file. Continue here when input fails (EOF or error)

13 Reading a Text File if (infile.eof()) { cout << endl << "End of file \n"; } else { cout << endl << "Error reading file\n"; } infile.close(); cout << "Normal termination\n"; cin.get(); // Keep window open. cin.get(); return 0; } Check cause of input failure Good! Bad!

14 Input Example Running

15 Observations >> for file input works just like it does for keyboard input Read the next word. Terminate input at whitespace. Probably not what was intended To read an entire line, use getline()

16 Chanages to Read Entire Line... // Input file is open while (infile.good()) { infile.getline(input_line, 500); cout << "Next line is: " << input_line << endl; }

17 File Input Example Running Now using getline() method

18 Reading Numbers from a File >> works as expected More or less So long as file format is what was expected.

19 Reading Numbers from a File // Input file is open while (infile.good()) { double value; infile >> value; cout << "Next input is: " << value << endl; } Change for numeric input

20 The Input File

21 Program read_file_numeric Running What is this?

22 Reading Numbers from a File // Input file is open while (infile.good()) { double value; infile >> value; cout << "Next input is: " << value << endl; } value is unchanged when EOF is reached.

23 Avoiding Bogus Input at EOF while (infile.good()) { double value; infile >> value; if (!infile.good()) { break; } cout << "Next input is: " << value << endl; } Check for error or EOF after attempting input and before using the result

24 Revised Program Running

25 Reading Integers from a Text File Integer Input File

26 Reading Integers from a Text File // Input file is open while (infile.good()) { int value; infile >> value; if (!infile.good()) { break; } cout << "Next input is: " << value << endl; } Only change

27 Integer File Input Example Running

28 What If Input Is Not As Expected?

29 Attempting to Read Bad Integer Input

30 Input Failure Invalid input puts the stream into the fail state. No further input will succeed unless we reset the state. Can hang in an endless loop if we just try to continue. Typically the best policy is to abort the program. Recovery is possible when it makes sense.

31 Summary File I/O in C++ is similar to console I/O Instantiate object to read or write. Call its methods to do I/O > work as with cout and cin. Check for EOF and errors on input Error handling is tricky!

32 Assignment Read about file I/O in the text Chapter 6, page 262 – 274 Try the examples from this presentation for yourself.