CS 1620
description
Transcript of CS 1620
![Page 1: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/1.jpg)
CS 1620
File I/O
![Page 2: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/2.jpg)
So far this semesterall input has been from keyboardall output has been to computer screen
these are just two examples of where to retrieve/send data
![Page 3: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/3.jpg)
ifstream – Input File Stream reads input from the location of your choice ifstream is a type
declare variables of type ifstreamuse variable for file input
![Page 4: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/4.jpg)
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();
![Page 5: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/5.jpg)
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<fstream>
![Page 6: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/6.jpg)
.open takes a null-terminated character array as
inputstring 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);
![Page 7: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/7.jpg)
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());
![Page 8: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/8.jpg)
.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<cstdlib> to use exit
![Page 9: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/9.jpg)
.closecloses 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();
![Page 10: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/10.jpg)
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".
![Page 11: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/11.jpg)
#include <iostream>#include <fstream>#include <cstdlib> 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".
![Page 12: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/12.jpg)
#include <iostream>#include <fstream>#include <cstdlib> 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
![Page 13: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/13.jpg)
#include <iostream>#include <fstream>#include <cstdlib> 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
![Page 14: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/14.jpg)
#include <iostream>#include <fstream>#include <cstdlib> 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
![Page 15: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/15.jpg)
#include <iostream>#include <fstream>#include <cstdlib> 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
![Page 16: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/16.jpg)
#include <iostream>#include <fstream>#include <cstdlib> 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
![Page 17: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/17.jpg)
#include <iostream>#include <fstream>#include <cstdlib> 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
![Page 18: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/18.jpg)
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".
![Page 19: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/19.jpg)
// 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".
![Page 20: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/20.jpg)
// 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".
![Page 21: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/21.jpg)
// 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".
![Page 22: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/22.jpg)
// 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".
![Page 23: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/23.jpg)
// 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".
![Page 24: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/24.jpg)
// 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".
![Page 25: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/25.jpg)
// 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".
![Page 26: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/26.jpg)
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 istreamhence, any operation supported by cin, is
also supported by infile the >> operatorgetline
![Page 27: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/27.jpg)
Example: Read in three values from a file called "data.txt", and print out the average of those three values.
![Page 28: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/28.jpg)
// 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!
![Page 29: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/29.jpg)
// 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:
![Page 30: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/30.jpg)
// 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.
![Page 31: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/31.jpg)
// 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.
![Page 32: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/32.jpg)
// 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.
![Page 33: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/33.jpg)
File Input: Unknown file sizeExample: read in all of the numbers from a
file, and print their average.
![Page 34: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/34.jpg)
// 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.
![Page 35: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/35.jpg)
Solution: loop through all values in the file
add each value read to a variable totalcount the number of values read
divide total by the number of values read
![Page 36: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/36.jpg)
// 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?
![Page 37: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/37.jpg)
.eof() .eof() returns true when the file has been
read past its endtypical format for .eof() loops
Step 1: read a value from filestreamStep 2: while ( filestream.eof() is not true)
Step 2.1 Do something with value Step 2.2 read another value from filestream
![Page 38: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/38.jpg)
// 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 filestreamStep 2: while ( filestream.eof() is not true)
Step 2.1 Do something with value Step 2.2 read another value from filestream
![Page 39: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/39.jpg)
// 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 filestreamStep 2: while ( filestream.eof() is not true)
Step 2.1 Do something with value Step 2.2 read another value from filestream
![Page 40: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/40.jpg)
// 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 filestreamStep 2: while ( filestream.eof() is not true)
Step 2.1 Do something with value Step 2.2 read another value from filestream
![Page 41: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/41.jpg)
// 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 filestreamStep 2: while ( filestream.eof() is not true)Step 2.1 Do something with value
Step 2.2 read another value from filestream
![Page 42: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/42.jpg)
// 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 filestreamStep 2: while ( filestream.eof() is not true)Step 2.1 Do something with value
Step 2.2 read another value from filestream
![Page 43: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/43.jpg)
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
![Page 44: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/44.jpg)
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();
![Page 45: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/45.jpg)
Example: Write your name, address, and phone number to a file called "name.txt"
![Page 46: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/46.jpg)
// 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) 456-7890" << endl;
outfile.close();
return 0;
}
If we were writing to the screen, we would do this.
![Page 47: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/47.jpg)
// 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) 456-7890" << endl;
outfile.close();
return 0;
}
Replace cout with outfile
![Page 48: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/48.jpg)
// 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) 456-7890" << endl;
outfile.close();
return 0;
}
![Page 49: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/49.jpg)
Kevin Grant1234 Main StreetDisneyland, California, 90210(123) 456-7890
name.txt
![Page 50: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/50.jpg)
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 reading2) Open a file called output.txt for writing3) Write each line in input.txt to output.txt4) Close files
![Page 51: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/51.jpg)
// 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;}
![Page 52: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/52.jpg)
// 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;}
![Page 53: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/53.jpg)
// 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;}
![Page 54: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/54.jpg)
// 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;}
![Page 55: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/55.jpg)
// 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;}
![Page 56: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/56.jpg)
// 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;}
![Page 57: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/57.jpg)
// 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;}
![Page 58: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/58.jpg)
// 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 filestreamStep 2: while ( filestream.eof() is not true)
Step 2.1 Do something with value Step 2.2 read another value from filestream
![Page 59: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/59.jpg)
// 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;}
![Page 60: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/60.jpg)
// 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;}
![Page 61: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/61.jpg)
// 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;}
![Page 62: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/62.jpg)
// 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;}
![Page 63: CS 1620](https://reader035.fdocuments.in/reader035/viewer/2022062423/56814e99550346895dbc4221/html5/thumbnails/63.jpg)
// 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;}