1 / 21

More About File Reading

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 {

lael
Télécharger la présentation

More About File Reading

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. 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);

  3. 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; }

  4. 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; }

  5. 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; }

  6. 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); }

  7. 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); }

  8. 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); }

  9. 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); }

  10. 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); }

  11. 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; }

  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

More Related