210 likes | 310 Views
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 {
E N D
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);
Reading the Entire File input.dat 3 11 34 56 3 14 12 6 124 -1 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; while (num != -1) { process_data(num); fin >> num; }
Reading the Entire File input.dat 3 11 34 56 3 14 12 6 124 -1 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; while (num != -1) { process_data(num); fin >> num; }
Reading the Entire File input.dat 3 11 34 56 3 14 12 6 124 -1 marks end of data #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; while (num != -1) { process_data(num); fin >> num; }
Reading the Entire File 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 // SIZE is size of dataset – known a priori for (inti = 0; i < SIZE; i++) { fin >> num; process_data(num); }
Reading the Entire File 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 // size of dataset unknown! while (fin >> num) // returns false when last { // data element read process_data(num); }
Reading the Entire File 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 while (fin >> num) { fin >> num; process_data(num); }
Reading the Entire File 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 while (fin >> num) { fin >> num; //skips every other datum process_data(num); }
Reading the Entire File input.dat 124663 Price 124645 Hurson 124687 Buechler 124752 Davis #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); }
Reading the Entire File input.dat 3 -1 34 56 3 14 12 6 124 Note: eof becomes trueonly after trying to read past the last datum #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; }
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; }