Presentation is loading. Please wait.

Presentation is loading. Please wait.

More About File Reading

Similar presentations


Presentation on theme: "More About File Reading"— Presentation transcript:

1 More About File Reading

2 Prompts: Not Needed input.dat 3 -1 34 56 3 14 12 6 124
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file do cout << “What is your next number? ”; } while (fin >> num); input.dat

3 Reading the Entire File
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; while (num != -1) process_data(num); } input.dat -1

4 Reading the Entire File
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; while (num != -1) process_data(num); } input.dat -1

5 Reading the Entire File
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; while (num != -1) process_data(num); } input.dat -1 marks end of data

6 Reading the Entire File
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file // SIZE is size of dataset – known a priori for (int i = 0; i < SIZE; i++) fin >> num; process_data(num); } input.dat

7 Reading the Entire File
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file // size of dataset unknown! while (fin >> num) // returns false when last { // data element read process_data(num); } input.dat

8 Reading the Entire File
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file while (fin >> num) fin >> num; process_data(num); } input.dat

9 Reading the Entire File
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file while (fin >> num) fin >> num; //skips every other datum process_data(num); } input.dat

10 Reading the Entire File
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file while (fin >> num) // gets integer data fin >> name; // gets name data process_data(num, name); } input.dat Price Hurson Buechler Davis

11 Reading the Entire File
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; // necessary for case of // empty file while (!fin.eof()) process_data(num); fin >> num; } input.dat Note: eof becomes true only after trying to read past the last datum

12 Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

13 Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

14 Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

15 Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

16 Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

17 Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

18 Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

19 Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

20 Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

21 End of Session


Download ppt "More About File Reading"

Similar presentations


Ads by Google