1 / 8

CS 105 Lecture 9 Files

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

will
Download Presentation

CS 105 Lecture 9 Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm

  2. 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.

  3. 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();

  4. 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();

  5. 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.

  6. 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.

  7. 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

  8. 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

More Related