Download presentation
Presentation is loading. Please wait.
1
File Streams and File I/O
2
Stream Operators Insertion Operator ‘<<’
As seen with cout << var Extraction Operator ‘>>’ As seen with cin >> var
3
Stream Declarations #include <fstream> using namespace std; int main() { ifstream fin; // streams data from a file ofstream fout; // streams data to a file
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
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”);
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
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);
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);
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);
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);
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);
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);
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);
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);
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);
16
Reading a File input.dat 3 -1 34 56 3 14 12 6 124
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat
17
Reading a File input.dat 3 -1 34 56 3 14 12 6 124
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat
18
Reading a File input.dat 3 -1 34 56 3 14 12 6 124
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat
19
Reading a File input.dat 3 -1 34 56 3 14 12 6 124
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat
20
Reading a File input.dat 3 -1 34 56 3 14 12 6 124
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat
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();
22
More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
#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; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
23
More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
#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; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
24
More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
#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; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
25
More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
#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; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
26
More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
#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; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
27
More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
#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; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
28
More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
#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; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87
29
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.dat A Intro to Programming B Intro to Programming C Intro to Programming A Discrete Mathematics B Discrete Mathematics A Data Structures B Data Structures
30
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.dat A Intro to Programming B Intro to Programming C Intro to Programming A Discrete Mathematics B Discrete Mathematics A Data Structures B Data Structures
31
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.dat A Intro to Programming B Intro to Programming C Intro to Programming A Discrete Mathematics B Discrete Mathematics A Data Structures B Data Structures
32
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.dat A Intro to Programming B Intro to Programming C Intro to Programming A Discrete Mathematics B Discrete Mathematics A Data Structures B Data Structures
33
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.dat A Intro to Programming B Intro to Programming C Intro to Programming A Discrete Mathematics B Discrete Mathematics A Data Structures B Data Structures
34
More Reads input.dat Price, Clayton Hurson, Ali Buechler, Matt
#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, Clayton Hurson, Ali Buechler, Matt Van Horn, Keith Walker, Betty Sue
35
End of Session
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.