View
45
Download
0
Category
Tags:
Preview:
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 Presentation
Citation preview
http://cs.mst.edu
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 #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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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();
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
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
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
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
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
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
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
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
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
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
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
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
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
http://cs.mst.edu
End of Session
Recommended