file streams and file i/o
DESCRIPTION
File Streams and File I/O. Stream Operators. Insertion Operator ‘> var. Stream Declarations. #include < fstream > using namespace std; int main() { ifstream fin; // streams data from a file - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/1.jpg)
http://cs.mst.edu
File Streams and File I/O
![Page 2: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/2.jpg)
http://cs.mst.edu
Stream Operators Insertion Operator ‘<<’
As seen with cout << var Extraction Operator ‘>>’
As seen with cin >> var
![Page 3: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/3.jpg)
http://cs.mst.edu
Stream Declarations #include <fstream> using namespace std;
int main() { ifstream fin; // streams data from a file ofstream fout; // streams data to a file
![Page 4: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/4.jpg)
http://cs.mst.edu
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
![Page 5: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/5.jpg)
http://cs.mst.edu
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”);
![Page 6: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/6.jpg)
http://cs.mst.edu
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
![Page 7: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/7.jpg)
http://cs.mst.edu
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);
![Page 8: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/8.jpg)
http://cs.mst.edu
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);
![Page 9: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/9.jpg)
http://cs.mst.edu
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);
![Page 10: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/10.jpg)
http://cs.mst.edu
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);
![Page 11: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/11.jpg)
http://cs.mst.edu
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);
![Page 12: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/12.jpg)
http://cs.mst.edu
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);
![Page 13: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/13.jpg)
http://cs.mst.edu
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);
![Page 14: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/14.jpg)
http://cs.mst.edu
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);
![Page 15: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/15.jpg)
http://cs.mst.edu
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);
![Page 16: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/16.jpg)
http://cs.mst.edu
Reading a File #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { string file; ifstream fin; int num;
... // code for opening the file
fin >> num; fin >> num; fin >> num; fin >> num; etc;
input.dat
3 -1 34 56 3 14 12 6 124
![Page 17: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/17.jpg)
http://cs.mst.edu
Reading a File #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { string file; ifstream fin; int num;
... // code for opening the file
fin >> num; fin >> num; fin >> num; fin >> num; etc;
input.dat
3 -1 34 56 3 14 12 6 124
![Page 18: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/18.jpg)
http://cs.mst.edu
Reading a File #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { string file; ifstream fin; int num;
... // code for opening the file
fin >> num; fin >> num; fin >> num; fin >> num; etc;
input.dat
3 -1 34 56 3 14 12 6 124
![Page 19: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/19.jpg)
http://cs.mst.edu
Reading a File #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { string file; ifstream fin; int num;
... // code for opening the file
fin >> num; fin >> num; fin >> num; fin >> num; etc;
input.dat
3 -1 34 56 3 14 12 6 124
![Page 20: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/20.jpg)
http://cs.mst.edu
Reading a File #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { string file; ifstream fin; int num;
... // code for opening the file
fin >> num; fin >> num; fin >> num; fin >> num; etc;
input.dat
3 -1 34 56 3 14 12 6 124
![Page 21: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/21.jpg)
http://cs.mst.edu
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();
![Page 22: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/22.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file string last, first; int age;
fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc.
input.dat
Price Clayton 12Hurson Ali 41Buechler Matt 87
![Page 23: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/23.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file string last, first; int age;
fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc.
input.dat
Price Clayton 12Hurson Ali 41Buechler Matt 87
![Page 24: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/24.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file string last, first; int age;
fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc.
input.dat
Price Clayton 12Hurson Ali 41Buechler Matt 87
![Page 25: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/25.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file string last, first; int age;
fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc.
input.dat
Price Clayton 12Hurson Ali 41Buechler Matt 87
![Page 26: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/26.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file string last, first; int age;
fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc.
input.dat
Price Clayton 12Hurson Ali 41Buechler Matt 87
![Page 27: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/27.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file string last, first; int age;
fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc.
input.dat
Price Clayton 12Hurson Ali 41Buechler Matt 87
![Page 28: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/28.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file string last, first; int age;
fin >> last; fin >> first; fin >> age; fin >> last; fin >> first; fin >> age; etc.
input.dat
Price Clayton 12Hurson Ali 41Buechler Matt 87
![Page 29: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/29.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file int class_num, course; char section; string title;
input.dat51324 53 A Intro to Programming51325 53 B Intro to Programming51326 53 C Intro to Programming51334 128 A Discrete Mathematics51335 128 B Discrete Mathematics51344 153 A Data Structures51345 153 B Data Structures
![Page 30: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/30.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file int class_num, course; char section; string title;
input.dat51324 53 A Intro to Programming51325 53 B Intro to Programming51326 53 C Intro to Programming51334 128 A Discrete Mathematics51335 128 B Discrete Mathematics51344 153 A Data Structures51345 153 B Data Structures
![Page 31: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/31.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file int class_num, course; char section; string title;
input.dat51324 53 A Intro to Programming51325 53 B Intro to Programming51326 53 C Intro to Programming51334 128 A Discrete Mathematics51335 128 B Discrete Mathematics51344 153 A Data Structures51345 153 B Data Structures
![Page 32: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/32.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file int class_num, course; char section; string title;
input.dat51324 53 A Intro to Programming51325 53 B Intro to Programming51326 53 C Intro to Programming51334 128 A Discrete Mathematics51335 128 B Discrete Mathematics51344 153 A Data Structures51345 153 B Data Structures
![Page 33: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/33.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file int class_num, course; char section; string title;
fin >> class_num; fin >> course; fin >> section; getline(fin, title, ‘\n’);
input.dat51324 53 A Intro to Programming51325 53 B Intro to Programming51326 53 C Intro to Programming51334 128 A Discrete Mathematics51335 128 B Discrete Mathematics51344 153 A Data Structures51345 153 B Data Structures
![Page 34: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/34.jpg)
http://cs.mst.edu
More Reads #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { ... // code for opening the file string last, first;
getline(fin, last, ‘,’); getline(fin, first, ‘\n’); etc.
input.dat
Price, ClaytonHurson, AliBuechler, MattVan Horn, KeithWalker, Betty Sue
![Page 35: File Streams and File I/O](https://reader035.vdocument.in/reader035/viewer/2022062816/56814d50550346895dba8611/html5/thumbnails/35.jpg)
http://cs.mst.edu
End of Session