Download presentation
Presentation is loading. Please wait.
1
Standard Input/Output Stream
// Header file #include <iostream> int size; float avg; cin >> size; // cin: Standard input stream // Processing cout << "Average is " << avg; // cout: standard output stream -----
2
Header File for File I/O
// File Stream #include <fstream> // Input File Stream struct ifstream // should be class { … } // Output File Stream struct ofstream // should be class -----
3
File Input/Output #include <fstream> int main() {
ifstream MyInput; ofstream yourOutFile; int x; MyInput.open(“P6.IN"); yourOutFile.open(“p6.out”); if (MyInput.fail() || !P6Out.good()) cout << "Error: Cannot open files!"; return 1; } MyInput >> x; while (!MyInput.eof()) x ++; yourOutFile << x; MyInput.close(); yourOutFile.close(); return 0;
4
File Input/Output // DO_01: Include header file for file I/O
int main() { // DO_02: declare variables for file I/O // DO_03: open file // DO_04: Check open operation // DO_05: Read value from the open file // DO_06: Fill condition: read until the end of file while ( ) // DO_07: Output value to a file // Read the next value from the file } // DO_08: Close files -----
5
Declare and Initialize Stream Variables
#include <fstream> int main() { ifstream MyInput(“P6.in"); ofstream P6Out(“P6.out"); if (MyInput.fail() || !P6Out.good()) cout << "Error: Cannot open files!"; return 1; } // Do work MyInput.close(); P6Out.close(); return 0; -----
6
Input File Name #include <fstream> int main() {
char file_name[21]; // Cannot use data type string for file_name ifstream MyInput; cout << “Enter input file name: ”; cin >> file_name; MyInput.open(file_name); // MyInput.open(“P6.IN"); // Check open operation if (!MyInput.good()) cout << "Error: Cannot open input file!"; return 1; } // Do work MyInput.close(); return 0;
7
Selecting I/O Streams #include <fstream> int main() {
ifstream MyInput; ofstream yourOutFile; // Open files for I/O // Check open operations cin = MyInput; cout = yourOutFile; cin >> x; cout << ++x; cout << x++; // Close files return 0; }
8
File Stream as Function Parameter
int main() { ifstream MyInput; if (!OpenFile(MyInput)) return 1; return 0; } bool OpenFile(ifstream& inFile) char file_name[21]; cin >> file_name; inFile.open(file_name); if (!inFile.good()) cout << "Error: Cannot open input file"; return false; else return true; -----
9
This Week Today Lab10 / Quiz10-1 / Prog6 Thursday
Lab11 / Prog6 / Test 3 Friday No Class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.