350 likes | 500 Views
File Streams and File I/O. Stream Operators. Insertion Operator ‘<<’ As seen with cout << var Extraction Operator ‘>>’ As seen with cin >> var. Stream Declarations. #include < fstream > using namespace std; int main() { ifstream fin; // streams data from a file
E N D
Stream Operators • Insertion Operator ‘<<’ • As seen with cout << var • Extraction Operator ‘>>’ • As seen with cin >> var
Stream Declarations #include <fstream> using namespace std; int main() { ifstream fin; // streams data from a file ofstreamfout; // streams data to a file
Stream Declarations #include <fstream> using namespace std; int main() { ifstreamfin(“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
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”);
Opening a File w/C-strings • File may not exist • File may be misspelled • Perhaps wrong directory • etc #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; ofstream fout; fin.open(“input.dat”); // !may not connect! fout.open(“output.dat”);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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; fin >> num; fin >> num; fin >> num; etc;
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; fin >> num; fin >> num; fin >> num; etc;
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; fin >> num; fin >> num; fin >> num; etc;
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; fin >> num; fin >> num; fin >> num; etc;
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; fin >> num; fin >> num; fin >> num; etc;
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();
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; fin >> last; fin >> first; fin >> age; etc.
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; fin >> last; fin >> first; fin >> age; etc.
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; fin >> last; fin >> first; fin >> age; etc.
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; fin >> last; fin >> first; fin >> age; etc.
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; fin >> last; fin >> first; fin >> age; etc.
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; fin >> last; fin >> first; fin >> age; etc.
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; fin >> last; fin >> first; fin >> age; etc.
More Reads input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file intclass_num, course; char section; string title;
More Reads input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file intclass_num, course; char section; string title;
More Reads input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file intclass_num, course; char section; string title;
More Reads input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file intclass_num, course; char section; string title;
More Reads input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file intclass_num, course; char section; string title; fin >> class_num; fin >> course; fin >> section; getline(fin, title, ‘\n’);
More Reads input.dat Price, Clayton Hurson, Ali Buechler, Matt Van Horn, Keith Walker, Betty Sue #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.