More About File Reading
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 -1 34 56 3 14 12 6 124
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 3 11 34 56 3 14 12 6 124 -1
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 3 11 34 56 3 14 12 6 124 -1
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 3 11 34 56 3 14 12 6 124 -1 marks end of data
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 3 -1 34 56 3 14 12 6 124
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 3 -1 34 56 3 14 12 6 124
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 3 -1 34 56 3 14 12 6 124
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 3 -1 34 56 3 14 12 6 124
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 124663 Price 124645 Hurson 124687 Buechler 124752 Davis
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 3 -1 34 56 3 14 12 6 124 Note: eof becomes true only after trying to read past the last datum
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
End of Session