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.

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

1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
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.
Chapter 5: Loops and Files.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
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.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Program Input and the Software Design Process ROBERT REAVES.
計算機程式語言 Lecture 8-1 國立臺灣大學生物機電系 8 8 I/O File Streams and Data Files.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Chapter 8 Data File Basics.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
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.
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,
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
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.
1 Chapter 4 Program Input and the Software Design Process.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
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.
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.
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?
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
File Processing.
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Lecture 9 Files, Pointers
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
File I/O.
Lecture 5A File processing Richard Gesick.
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
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
Text Files.
Presentation transcript:

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 retrieve/send data

ifstream – Input File Stream reads input from the location of your choice ifstream is a type declare variables of type ifstream use variable for file input

Steps to read from a file: 1. declare a variable infile of type ifstream 2. call infile.open, and send the filename as its parameter 3. check to see if the open command worked with infile.fail 4. use infile to read in data 5. call infile.close to close the stream ifstream infile; infile.open("prices.dat"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } // read data from infile infile.close();

ifstream variable declared like any other variable ifstream infile; can use any legal variable name that you like I will use infile for all of my examples ifstream kev; // legal, but not a good choice kev.open("kev.txt"); requires #include

.open takes a null-terminated character array as input string literal: infile.open("prices.dat"); character array variable char filename[] = "prices.dat"; infile.open(filename); will a string variable work? string filename = "prices.dat"; infile.open(filename);

c_str a string variable does not hold a null-terminated character array string data type provides a member function called c_str() returns a null-terminated character array representing the string to use, just affix.c_str() to the variable name string filename = "prices.dat"; infile.open(filename.c_str());

.fail returns true if the file has been opened, false otherwise if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } exit terminates the program, and returns its argument value to the operating system must #include to use exit

.close closes the current file stream good programming practice to close files when you're done with them O/S only allows finite number of open files infile.close();

Example 1: Open a file called "prices.dat". If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

#include using namespace std; int main() { ifstream infile; infile.open("prices.dat"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Example 1: Open a file called "prices.dat". If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

#include using namespace std; int main() { ifstream infile; infile.open("prices.dat"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Example 1: Open a file called "prices.dat". If the file opens, print the message "File successfully opened!". If not, print "File failed to open". Steps to read from a file: 1. declare a variable infile of type ifsteam 2. call infile.open, and send the filename as its parameter 3. check to see if the open command worked with infile.fail 4. use infile to read in data 5. call infile.close to close the stream

#include using namespace std; int main() { ifstream infile; infile.open("prices.dat"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Example 1: Open a file called "prices.dat". If the file opens, print the message "File successfully opened!". If not, print "File failed to open". Steps to read from a file: 1. declare a variable infile of type ifsteam 2. call infile.open, and send the filename as its parameter 3. check to see if the open command worked with infile.fail 4. use infile to read in data 5. call infile.close to close the stream

#include using namespace std; int main() { ifstream infile; infile.open("prices.dat"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Example 1: Open a file called "prices.dat". If the file opens, print the message "File successfully opened!". If not, print "File failed to open". Steps to read from a file: 1. declare a variable infile of type ifsteam 2. call infile.open, and send the filename as its parameter 3. check to see if the open command worked with infile.fail 4. use infile to read in data 5. call infile.close to close the stream

#include using namespace std; int main() { ifstream infile; infile.open("prices.dat"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Example 1: Open a file called "prices.dat". If the file opens, print the message "File successfully opened!". If not, print "File failed to open". Steps to read from a file: 1. declare a variable infile of type ifsteam 2. call infile.open, and send the filename as its parameter 3. check to see if the open command worked with infile.fail 4. use infile to read in data 5. call infile.close to close the stream

#include using namespace std; int main() { ifstream infile; infile.open("prices.dat"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Example 1: Open a file called "prices.dat". If the file opens, print the message "File successfully opened!". If not, print "File failed to open". Steps to read from a file: 1. declare a variable infile of type ifsteam 2. call infile.open, and send the filename as its parameter 3. check to see if the open command worked with infile.fail 4. use infile to read in data 5. call infile.close to close the stream

#include using namespace std; int main() { ifstream infile; infile.open("prices.dat"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Example 1: Open a file called "prices.dat". If the file opens, print the message "File successfully opened!". If not, print "File failed to open". Steps to read from a file: 1. declare a variable infile of type ifsteam 2. call infile.open, and send the filename as its parameter 3. check to see if the open command worked with infile.fail 4. use infile to read in data 5. call infile.close to close the stream

Example 2: Read a filename from the user, and open that file for reading. If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

// included libraries: iostream, fstream, cstlib, string int main() { string filename; cout << "Please enter the name of the file: "; cin >> filename; ifstream infile; infile.open(filename.c_str()); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Read a filename from the user, and open that file for reading. If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

// included libraries: iostream, fstream, cstlib, string int main() { string filename; cout << "Please enter the name of the file: "; cin >> filename; ifstream infile; infile.open(filename.c_str()); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Read a filename from the user, and open that file for reading. If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

// included libraries: iostream, fstream, cstlib, string int main() { string filename; cout << "Please enter the name of the file: "; cin >> filename; ifstream infile; infile.open(filename.c_str()); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Read a filename from the user, and open that file for reading. If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

// included libraries: iostream, fstream, cstlib, string int main() { string filename; cout << "Please enter the name of the file: "; cin >> filename; ifstream infile; infile.open(filename.c_str()); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Read a filename from the user, and open that file for reading. If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

// included libraries: iostream, fstream, cstlib, string int main() { string filename; cout << "Please enter the name of the file: "; cin >> filename; ifstream infile; infile.open(filename.c_str()); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Read a filename from the user, and open that file for reading. If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

// included libraries: iostream, fstream, cstlib, string int main() { string filename; cout << "Please enter the name of the file: "; cin >> filename; ifstream infile; infile.open(filename.c_str()); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Read a filename from the user, and open that file for reading. If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

// included libraries: iostream, fstream, cstlib, string int main() { string filename; cout << "Please enter the name of the file: "; cin >> filename; ifstream infile; infile.open(filename.c_str()); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "File successfully opened!" << endl; infile.close(); return 0; } Read a filename from the user, and open that file for reading. If the file opens, print the message "File successfully opened!". If not, print "File failed to open".

How to read from a file ifstream is a subtype of istream this means that anything you can do with an istream, you can do with an ifstream cin is an istream hence, any operation supported by cin, is also supported by infile the >> operator getline

Example: Read in three values from a file called "data.txt", and print out the average of those three values.

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val1, val2, val3; infile >> val1 >> val2 >> val3; cout << "The average of these values is: " << (val1 + val2 + val3)/3 << endl; infile.close(); return 0; } Example: Read in three values from a file called "data.txt", and print out the average of those three values. Code for reading in values goes here!

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val1, val2, val3; cin >> val1 >> val2 >> val3; cout << "The average of these values is: " << (val1 + val2 + val3)/3 << endl; infile.close(); return 0; } Example: Read in three values from a file called "data.txt", and print out the average of those three values. If we were reading from the keyboard, code would look like this:

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val1, val2, val3; cin >> val1 >> val2 >> val3; cout << "The average of these values is: " << (val1 + val2 + val3)/3 << endl; infile.close(); return 0; } Example: Read in three values from a file called "data.txt", and print out the average of those three values. Replace cin with infile, and you'll read from the file, instead of the keyboard.

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val1, val2, val3; infile >> val1 >> val2 >> val3; cout << "The average of these values is: " << (val1 + val2 + val3)/3 << endl; infile.close(); return 0; } Example: Read in three values from a file called "data.txt", and print out the average of those three values. Replace cin with infile, and you'll read from the file, instead of the keyboard.

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val1, val2, val3; infile >> val1 >> val2 >> val3; cout << "The average of these values is: " << (val1 + val2 + val3)/3 << endl; infile.close(); return 0; } Example: Read in three values from a file called "data.txt", and print out the average of those three values.

File Input: Unknown file size Example: read in all of the numbers from a file, and print their average.

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val1, val2, val3; infile >> val1 >> val2 >> val3; cout << "The average of these values is: " << (val1 + val2 + val3)/3 << endl; infile.close(); return 0; } Example: Read in three values from a file called "data.txt", and print out the average of those three values. This accommodates exactly three values. if "test.txt" contains more than 3 values, they are ignored if "test.txt" contains less than 3 values, program doesn't fill val3.

Solution: loop through all values in the file add each value read to a variable total count the number of values read divide total by the number of values read

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double total = 0; int count = 0; infile >> val; while ( ) { total += val; count++; infile >> val; } if (count == 0) { cout << "No values to average!" << endl; } else { cout << "The average of these values is: " << total / count << endl; } infile.close(); return 0; } What goes here?

.eof().eof() returns true when the file has been read past its end typical format for.eof() loops Step 1: read a value from filestream Step 2: while ( filestream.eof() is not true) Step 2.1 Do something with value Step 2.2 read another value from filestream

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double total = 0; int count = 0; infile >> val; while ( ) { total += val; count++; infile >> val; } if (count == 0) { cout << "No values to average!" << endl; } else { cout << "The average of these values is: " << total / count << endl; } infile.close(); return 0; } Step 1: read a value from filestream Step 2: while ( filestream.eof() is not true) Step 2.1 Do something with value Step 2.2 read another value from filestream

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val, total = 0; int count = 0; infile >> val; while ( ) { total += val; count++; infile >> val; } if (count == 0) { cout << "No values to average!" << endl; } else { cout << "The average of these values is: " << total / count << endl; } infile.close(); return 0; } Step 1: read a value from filestream Step 2: while ( filestream.eof() is not true) Step 2.1 Do something with value Step 2.2 read another value from filestream

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val, total = 0; int count = 0; infile >> val; while ( !infile.eof() ) { total += val; count++; infile >> val; } if (count == 0) { cout << "No values to average!" << endl; } else { cout << "The average of these values is: " << total / count << endl; } infile.close(); return 0; } Step 1: read a value from filestream Step 2: while ( filestream.eof() is not true) Step 2.1 Do something with value Step 2.2 read another value from filestream

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val, total = 0; int count = 0; infile >> val; while ( !infile.eof() ) { total += val; count++; infile >> val; } if (count == 0) { cout << "No values to average!" << endl; } else { cout << "The average of these values is: " << total / count << endl; } infile.close(); return 0; } Step 1: read a value from filestream Step 2: while ( filestream.eof() is not true) Step 2.1 Do something with value Step 2.2 read another value from filestream

// included libraries: iostream, fstream, cstlib int main() { ifstream infile; infile.open("test.txt"); if (infile.fail()) { cout << "File failed to open" << endl; exit(1); } double val, total = 0; int count = 0; infile >> val; while ( !infile.eof() ) { total += val; count++; infile >> val; } if (count == 0) { cout << "No values to average!" << endl; } else { cout << "The average of these values is: " << total / count << endl; } infile.close(); return 0; } Step 1: read a value from filestream Step 2: while ( filestream.eof() is not true) Step 2.1 Do something with value Step 2.2 read another value from filestream

File Output so far, all of our information has gone to the screen we can redirect this to a file, in a similar manner as redirecting input from a file use ofstream variable ofstream is a type, of subtype ostream cout is an ostream variable anything you can do with cout, you can do with an ofstream variable this includes <<, and all formatting flags

Steps to write to a file: 1. declare a variable outfile of type ofstream 2. call outfile.open, and send the filename as its parameter 3. check to see if the open command worked with outfile.fail 4. use outfile to output data 5. call outfile.close to close the stream ofstream outfile; outfile.open("prices.dat"); if (outfile.fail()) { cout << "File failed to open" << endl; exit(1); } // write data to outfile outfile.close();

Example: Write your name, address, and phone number to a file called "name.txt"

// included libraries: iostream, fstream, cstlib int main() { ofstream outfile; outfile.open("name.txt"); if (outfile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "Kevin Grant" << endl; cout << "1234 Main Street" << endl; cout << "Disneyland, California, 90210" << endl; cout << "(123) " << endl; outfile.close(); return 0; } If we were writing to the screen, we would do this.

// included libraries: iostream, fstream, cstlib int main() { ofstream outfile; outfile.open("name.txt"); if (outfile.fail()) { cout << "File failed to open" << endl; exit(1); } cout << "Kevin Grant" << endl; cout << "1234 Main Street" << endl; cout << "Disneyland, California, 90210" << endl; cout << "(123) " << endl; outfile.close(); return 0; } Replace cout with outfile

// included libraries: iostream, fstream, cstlib int main() { ofstream outfile; outfile.open("name.txt"); if (outfile.fail()) { cout << "File failed to open" << endl; exit(1); } outfile << "Kevin Grant" << endl; outfile << "1234 Main Street" << endl; outfile << "Disneyland, California, 90210" << endl; outfile << "(123) " << endl; outfile.close(); return 0; }

Kevin Grant 1234 Main Street Disneyland, California, (123) name.txt

Example: Copy the contents of a file called "input.txt" to a file called "output.txt" Steps: 1) Open a file called input.txt for reading 2) Open a file called output.txt for writing 3) Write each line in input.txt to output.txt 4) Close files

// included libraries: iostream, fstream, cstlib, string int main() { 1) Open a file called input.txt for reading 2) Open a file called output.txt for writing 3) Write each line in input.txt to output.txt 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { 1) Open a file called input.txt for reading 2) Open a file called output.txt for writing 3) Write each line in input.txt to output.txt 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } 2) Open a file called output.txt for writing 3) Write each line in input.txt to output.txt 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } 2) Open a file called output.txt for writing 3) Write each line in input.txt to output.txt 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } ofstream outfile; outfile.open("output.txt"); if (outfile.fail()) { cout << "output.txt failed to open." << endl; exit(1); } 3) Write each line in input.txt to output.txt 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } ofstream outfile; outfile.open("output.txt"); if (outfile.fail()) { cout << "output.txt failed to open." << endl; exit(1); } 3) Write each line in input.txt to output.txt 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } ofstream outfile; outfile.open("output.txt"); if (outfile.fail()) { cout << "output.txt failed to open." << endl; exit(1); } 3) For each line in input.txt 3.1) Write line to output.txt 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } ofstream outfile; outfile.open("output.txt"); if (outfile.fail()) { cout << "output.txt failed to open." << endl; exit(1); } 3) For each line in input.txt 3.1) Write line to output.txt 4) Close files return 0; } Step 1: read a value from filestream Step 2: while ( filestream.eof() is not true) Step 2.1 Do something with value Step 2.2 read another value from filestream

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } ofstream outfile; outfile.open("output.txt"); if (outfile.fail()) { cout << "output.txt failed to open." << endl; exit(1); } string buffer; getline(infile, buffer); while ( !infile.eof() ) { 3.1) Write line to output.txt getline(infile, buffer); } 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } ofstream outfile; outfile.open("output.txt"); if (outfile.fail()) { cout << "output.txt failed to open." << endl; exit(1); } string buffer; getline(infile, buffer); while ( !infile.eof() ) { 3.1) Write line to output.txt getline(infile, buffer); } 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } ofstream outfile; outfile.open("output.txt"); if (outfile.fail()) { cout << "output.txt failed to open." << endl; exit(1); } string buffer; getline(infile, buffer); while ( !infile.eof() ) { outfile << buffer << endl; getline(infile, buffer); } 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } ofstream outfile; outfile.open("output.txt"); if (outfile.fail()) { cout << "output.txt failed to open." << endl; exit(1); } string buffer; getline(infile, buffer); while ( !infile.eof() ) { outfile << buffer << endl; getline(infile, buffer); } 4) Close files return 0; }

// included libraries: iostream, fstream, cstlib, string int main() { ifstream infile; infile.open("input.txt"); if (infile.fail()) { cout << "input.txt failed to open." << endl; exit(1); } ofstream outfile; outfile.open("output.txt"); if (outfile.fail()) { cout << "output.txt failed to open." << endl; exit(1); } string buffer; getline(infile, buffer); while ( !infile.eof() ) { outfile << buffer << endl; getline(infile, buffer); } infile.close(); outfile.close(); return 0; }