Содержание
- 2. Review: File I/O Header file: #include Declaring variables You declare a variable of type ifstream for
- 3. Streams as parameters Streams are ALWAYS pass-by-reference (&) Example: Function to open files: void openOutputFile (ofstream&
- 4. Example: writing a line of text #include #include using namespace std; int main() { ofstream fout(“greeting.txt”);
- 5. Example: reading a line of text #include #include #include using namespace std; int main() { ifstream
- 6. What can do with your stream? For ofstream Use Use I/O manipulators – don’t forget #include
- 7. Reading through a file Read through the file with getline can be done with simple loop
- 8. Your turn… Pair up with your neighbor to write this function: Write this function called skipWhite
- 9. Solution to exercise void skipWhite(ifstream& fin) { int ch; while(true) { ch = fin.peek(); if (
- 10. Solution to exercise void skipWhite(ifstream& fin) { int ch; while(true) { ch = fin.peek(); if (
- 11. Can you mix getline and >>? What does getline do? Read characters into string variable until
- 12. ifstream fin; string str, line; fin.open("afile.txt"); getline(fin, line); fin >> str; cout cout fin.close(); fin.open("afile.txt"); fin
- 13. Behind the scenes with ofstream By default, what you write to an ofstream is first saved
- 14. Behind the scenes with ifstream Reading from a file ifstream fin(“myFile”); string line; getline(fin, line);
- 15. Behind the scenes with ifstream An ifstream object reads a whole block of data from the
- 16. Behind the scenes with ifstream An ifstream object reads a whole block of data from the
- 17. What about wide characters? Use wifstream and wofstream instead…
- 19. Скачать презентацию