1 / 20

CS114-009 Class 13

CS114-009 Class 13. Today File I/O (reading from and writing to files) Reading until EOF Formatting output Nested Loops Announcements Programming project #4: will be posted by Thursday is due by midnight Oct. 23 Exam 2 is Oct. 28 during class

Download Presentation

CS114-009 Class 13

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. CS114-009 Class 13 • Today • File I/O (reading from and writing to files) • Reading until EOF • Formatting output • Nested Loops • Announcements • Programming project #4: will be posted by Thursday is due by midnight Oct. 23 • Exam 2 is Oct. 28 during class • Read pages 110-117, 317 – 331 for next time

  2. Up until now all reads from “standard input” using cin >> … all writes to “standard output” using cout << … Want to expand this Read from any input Write to any output C++ input/output uses a “stream” Flow of characters cin : data flows into pgm cout : data flows from pgm Can also define streams of input/output with files Input Overview

  3. Reading/Writing to files • Need an identifier to use when reading from (or writing to) a file Can’t use cin and cout, they go to standard input/output • ofstream outFile; • ifstream inFile; • Need to associate this variable with an actual file • outFile.open(“results.txt”); • inFile.open(“data.txt”); • Same general syntax when reading/writing • outFile << “answer is :” << x << endl; • inFile >> x >> y; • Close this file once you are done • outFile.close( ); inFile.close();

  4. #include <iostream> #include <fstream> using namespace std; int main( ) { int value, temp = 1; ifstream Alabama; Alabama.open("data.txt"); while (temp < 10) { Alabama >> value; cout << "Read = " << value << endl; temp = temp + 1; } Alabama.close( ); return 0; } #include <iostream> #include <fstream> using namespace std; int main( ) { int temp = 1; ofstream bigAL; bigAL.open("results.txt"); while (temp < 10) { bigAL << temp << endl; temp = temp + 1; } bigAL.close( ); return 0; } Examples

  5. In-Class Exercise Write a C++ program that will read the ten numbers shown below from the file data.txt and write the even numbers to the file even.txt and the write the odd numbers to the file odd.txt. 5 8 10 13 15 20 23 25 29 30 You can create the input file data.txt by either1) using Notepad2) File / Add New Item / <select a text file> Make sure your input data file is in the same directory as your C++ project.

  6. Summary: File-based I/O • For each file used, need a variable that links the C++ program with a specific external file C++ Program #include <iostream>#include <fstream>int main( ) { ifstream inFile; ofstream outFile; inFile.open(“data.txt”); outFile.open(“results.txt”); inFile >> … ; outFile << … ; data.txt results.txt

  7. Up until now Either Knew exactly how many input values there were Count-controlled loops Or Knew what the last item in the data looked like Sentinel-controlled loops What if you don’t know when the data ends (and what the last item is) ? Read until EOF Terminology: read until “end-of-file” (EOF) Two solutions let the “read” fail check for end-of-file How does the program see the “EOF”? Reading from filesystem detects the end of the file Reading from cininput a CTRL-d or CTRL-z Reading input

  8. Method #2: Test for EOF #include <iostream> using namespace std; int main( ) {int a;cin >> a;while ( !cin.eof( ) ) { cout << a << endl; cin >> a;}cout << "done" << endl;return 0; } // signal EOF via CTRL-z Method #1 : Let read fail #include <iostream> using namespace std; int main( ) { int a; while ( cin >> a) { cout << a << endl; } cout << "done" << endl; return 0; } // signal EOF via CTRL-d Read until you hit EOF

  9. Class Exercise • Write a C++ program that reads integers from standard input until EOF, printing the total and the average value once the user enters EOF. Your average value should be a double. • Modify your program to read from the file data.txt and output to the screen.

  10. Program output is “presented” to the user. Want professional look Alignment justification All output streams can be formatted. cout File output streams Specify flags for presentation Field width Precision Left justify Right justify Scientific notation Formatting Output

  11. 16 1024 16 1024 1024 3.141590e+000 3.14159 3.1 Comments Width only for next output item Will print entire number if field is too small int num0 = 16; int num1 = 1024; cout.width(5); cout << num0 << endl; cout << num1 << endl; cout.width(8); cout.setf(ios::right); cout << num0 << endl; cout << num1 << endl; cout.width(2); cout << num1 << endl; double num2 = 3.14159; cout.setf(ios::scientific); cout << num2 << endl; cout.setf(ios::fixed); cout << num2 << endl; cout.precision(2); cout << num2 << endl; Formatting example

  12. Just specify the flags needed to give you the desired format Can “unset” these flags later on (if needed)cout.unsetf(ios::showpos); Lots of flags exist. Examples Print integers right-justified and always show the “+” sign cout.setf(ios::right); cout.setf(ios::showpos); Print doubles using scientific notation cout.setf(ios::scientific); Print next integer in a field of width 8 cout.width(8); Rules for Formatting

  13. Integer Values Show “+” w/positive num setf(ios::showpos) Specifies width (only for the next value output) width(N) left-justify output setf(ios::left) right-justify output setf(ios::right) Real Values Fixed decimal notation setf(ios::fixed) Scientific notation setf(ios::scientific) Show decimal point setf(ios::showpoint) Show “+” w/positive num setf(ios::showpos) Set decimal places precision(N) Basic formatting alternatives

  14. int main( ) { int a = 5, loop = 3; while (loop <= 8) { cout.width(loop); cout.setf(ios::right); if (loop%2 == 0) cout.setf(ios::showpos);else cout.unsetf(ios::showpos); cout << a << endl; loop = loop + 1; a = a * 2; } return 0; } int main( ) { double z=9.43214321; int loop = 3; while (loop < 8) { cout.precision(loop); if (loop == 6) cout.setf(ios::left); cout.width(15-loop); cout << z << endl; loop = loop + 1; } return 0; } Class Exercises – Trace then run

  15. Write a program that generates the output below: 1 10 100 1000 10000 100000 Write a program that generates the output below: 0 1000 250 750 500 500 750 250 1000 0 Class Exercises

  16. Remember: If and while statements can be nested inside each other Can nest as deeply as needed by algorithm Can nest in any order Nesting - Review

  17. #include <iostream> using namespace std; int main( ) { for (int a=1; a<6; a++) { for (int b=1; b<6; b++) { cout.width(4); cout << a*b; } cout << endl; } return 0; } 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 Example: multiplication table

  18. 1 2 4 3 9 27 4 16 64 256 5 25 125 625 3125 #include <iostream> #include <cmath> using namespace std; int main( ) { for (int a=1; a<6; a++) { for (int b=1; b<=a; b++) { cout.width(5); cout << pow(a,b); } cout << endl; } return 0; } Example: powers, up to NN

  19. Class Exercise • Write C++ programs that use nested for loops to display one of the following designs: XXXXXX XXXXXXXXXX XXXXX XXXXX XXXXXXXX X X XXXX XXXXXX X X XXX XXXX X X XX XX X X X XXXXX

  20. End of Class 13

More Related