file streams and file i o
play

File Streams and File I/O http://cs.mst.edu Stream Operators - PowerPoint PPT Presentation

File Streams and File I/O http://cs.mst.edu Stream Operators Insertion Operator << As seen with cout << var Extraction Operator >> As seen with cin >> var http://cs.mst.edu Stream Declarations


  1. File Streams and File I/O http://cs.mst.edu

  2. Stream Operators  Insertion Operator ‘<<’  As seen with cout << var  Extraction Operator ‘>>’  As seen with cin >> var http://cs.mst.edu

  3. Stream Declarations #include <fstream> using namespace std; int main() { ifstream fin; // streams data from a file ofstream fout; // streams data to a file http://cs.mst.edu

  4. Stream Declarations #include <fstream> using namespace std; int main() { ifstream fin(“input.dat”); //connects this stream // to an existing data // file in the same // directory ofstream fout (“output.dat”); //creates a text file // in the same // directory http://cs.mst.edu

  5. Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; ofstream fout; fin.open (“input.dat”); // !may not connect! fout.open (“output.dat”); http://cs.mst.edu

  6. Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; ofstream fout; fin.open (“input.dat”); // !may not connect! fout.open (“output.dat”); • File may not exist • File may be misspelled • Perhaps wrong directory • etc http://cs.mst.edu

  7. Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do { fin.clear(); cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin); http://cs.mst.edu

  8. Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do { fin.clear(); // clears the fail bit – allows retry cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin); http://cs.mst.edu

  9. Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do { fin.clear(); // clears the fail bit – allows retry cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin); http://cs.mst.edu

  10. Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do { fin.clear(); // clears the fail bit – allows retry cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin); http://cs.mst.edu

  11. Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do { fin.clear(); // clears the fail bit – allows retry cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin); http://cs.mst.edu

  12. Opening a File w/std::strings #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; do { fin.clear(); cout << “enter name of file to connect to: “; cin >> file; fin.open(file.c_str()); } while(!fin); http://cs.mst.edu

  13. Opening a File w/std::strings #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; do { fin.clear(); cout << “enter name of file to connect to: “; cin >> file; fin.open(file.c_str()); } while(!fin); http://cs.mst.edu

  14. Opening a File w/std::strings #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; do { fin.clear(); cout << “enter name of file to connect to: “; cin >> file; fin.open(file.c_str()); } while(!fin); http://cs.mst.edu

  15. Opening a File w/std::strings #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; do { fin.clear(); cout << “enter name of file to connect to: “; cin >> file; fin.open(file.c_str()); } while(!fin); http://cs.mst.edu

  16. Reading a File #include <iostream> #include <fstream> input.dat #include <string> using namespace std; 3 -1 34 56 3 14 int main() { 12 6 124 string file; ifstream fin; int num; ... // code for opening the file fin >> num; fin >> num; fin >> num; fin >> num; etc; http://cs.mst.edu

  17. Reading a File #include <iostream> #include <fstream> input.dat #include <string> using namespace std; 3 -1 34 56 3 14 int main() { 12 6 124 string file; ifstream fin; int num; ... // code for opening the file fin >> num; fin >> num; fin >> num; fin >> num; etc; http://cs.mst.edu

  18. Reading a File #include <iostream> #include <fstream> input.dat #include <string> using namespace std; 3 -1 34 56 3 14 int main() { 12 6 124 string file; ifstream fin; int num; ... // code for opening the file fin >> num; fin >> num; fin >> num; fin >> num; etc; http://cs.mst.edu

  19. Reading a File #include <iostream> #include <fstream> input.dat #include <string> using namespace std; 3 -1 34 56 3 14 int main() { 12 6 124 string file; ifstream fin; int num; ... // code for opening the file fin >> num; fin >> num; fin >> num; fin >> num; etc; http://cs.mst.edu

  20. Reading a File #include <iostream> #include <fstream> input.dat #include <string> using namespace std; 3 -1 34 56 3 14 int main() { 12 6 124 string file; ifstream fin; int num; ... // code for opening the file fin >> num; fin >> num; fin >> num; fin >> num; etc; http://cs.mst.edu

  21. Closing a File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file ... // code for reading from the file fin.close(); http://cs.mst.edu

  22. More Reads #include <iostream> #include <fstream> input.dat #include <string> using namespace std; Price Clayton 12 Hurson Ali 41 int main() { Buechler Matt 87 ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc. http://cs.mst.edu

  23. More Reads #include <iostream> #include <fstream> input.dat #include <string> using namespace std; Price Clayton 12 Hurson Ali 41 int main() { Buechler Matt 87 ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc. http://cs.mst.edu

  24. More Reads #include <iostream> #include <fstream> input.dat #include <string> using namespace std; Price Clayton 12 Hurson Ali 41 int main() { Buechler Matt 87 ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc. http://cs.mst.edu

  25. More Reads #include <iostream> #include <fstream> input.dat #include <string> using namespace std; Price Clayton 12 Hurson Ali 41 int main() { Buechler Matt 87 ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc. http://cs.mst.edu

  26. More Reads #include <iostream> #include <fstream> input.dat #include <string> using namespace std; Price Clayton 12 Hurson Ali 41 int main() { Buechler Matt 87 ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc. http://cs.mst.edu

  27. More Reads #include <iostream> #include <fstream> input.dat #include <string> using namespace std; Price Clayton 12 Hurson Ali 41 int main() { Buechler Matt 87 ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc. http://cs.mst.edu

  28. More Reads #include <iostream> #include <fstream> input.dat #include <string> using namespace std; Price Clayton 12 Hurson Ali 41 int main() { Buechler Matt 87 ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc. http://cs.mst.edu

Recommend


More recommend