80 likes | 179 Views
CS 105 Lecture 9 Files. Version of Mon, Mar 28, 2011, 3:13 pm. Files. Data in main memory is “volatile” File: Place for “permanent” data storage C: drive, A: drive, flash drive, etc. In C++, files can be read different ways
E N D
CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm
Files • Data in main memory is “volatile” • File: Place for “permanent” data storage • C: drive, A: drive, flash drive, etc. • In C++, files can be read different ways • Stream input (or output) lets us read (or write) a sequence of data. • We also use streams (cin, cout) to model keyboard input and text output.
Output File Streams • In C++, the programmer can declare output streams and link them to output files. • #include <iostream> • #include <fstream> • using namespace std; • .... • int num1, num2, num3; • cout << "Enter 3 numbers for datafile: "; • cin >> num1 >> num2 >> num3; • ofstream outputStream; • outputStream.open("datafile.txt"); • outputStream << num1 << " "; • outputStream << num2 << " "; • outputStream << num3 << endl; • outputStream.close();
Input File Streams • In C++, the programmer can declare input streams and link them to input files. • #include <iostream> • #include <fstream> • using namespace std; • .... • int num1, num2, num3; • ifstream inputStream; • inputStream.open("datafile.txt"); • inputStream >> num1 >> num2 >> num3; • inputStream.close();
To Read or Write from a File • Declare the file stream object. • I.e., give it a name. • Associate a file name with the file stream object by opening the file. • Read or write to the file using << or >> operators. • Close the file.
Manipulators • Used to format output neatly: • Left justification (columns of names) • Right justification (columns of numbers) • Minimum field length (padding) • Decimal places • For more information, Malik pgs. 135-145 • There's also an older style of formatting that uses "printf" and "scanf" that has been copied into Java.
Example of Output Manipulation • #include <iostream> • #include <fstream> • #include <string> • #include <iomanip> • using namespace std; • int main() • { • ifstream input; • int loops, ival, i; • float fval; • string name; • cout << showpoint << scientific; cout << setprecision(2); • input.open("mydata.txt"); • input >> loops; • for (i=0; i < loops; i++) • { • input >> ival; • input >> fval; • input >> name; • cout << right; • cout << setw(4) << ival << " "; cout << setw(12) << fval << " "; cout << left; cout << left; cout << setw(10) << name << endl; } return(0); } Output 8 9.30e+000 Jon 6 1.43e+001 Bill 0 3.57e+010 Mary -23 -4.55e+000 Smith -3 -4.00e+003 xyz
Example with File Input • #include <iostream> • #include <fstream> • #include <string> • #include <iomanip> • using namespace std; • int main() • { • ifstream input; • int loops, ival, i; • float fval; • string name; • cout << showpoint • << scientific • << setprecision(2); • input.open("mydata.txt"); • input >> loops; • for (i=0; i < loops; i++) • { • input >> ival; • input >> fval; • input >> name; • cout << right; cout << setw(4) << ival << " "; cout << setw(12) << fval << " "; cout << left; cout << setw(10) << name << endl; } return(0); } mydata.txt file: 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz Output: 8 9.30e+00 Jon 6 1.43e+01 Bill 0 3.57e+10 Mary -23 -4.55e+00 Smith -3 -4.00e+03 xyz