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

file streams and file i o
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

http://cs.mst.edu

File Streams and File I/O

slide-2
SLIDE 2

http://cs.mst.edu

Stream Operators

  • Insertion Operator ‘<<’
  • As seen with cout << var
  • Extraction Operator ‘>>’
  • As seen with cin >> var
slide-3
SLIDE 3

http://cs.mst.edu

Stream Declarations

#include <fstream> using namespace std; int main() { ifstream fin; // streams data from a file

  • fstream fout; // streams data to a file
slide-4
SLIDE 4

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

  • fstream fout(“output.dat”); //creates a text file

// in the same // directory

slide-5
SLIDE 5

http://cs.mst.edu

Opening a File w/C-strings

#include <iostream> #include <fstream> using namespace std; int main() { ifstream fin;

  • fstream fout;

fin.open(“input.dat”); // !may not connect! fout.open(“output.dat”);

slide-6
SLIDE 6

http://cs.mst.edu

Opening a File w/C-strings

#include <iostream> #include <fstream> using namespace std; int main() { ifstream fin;

  • fstream fout;

fin.open(“input.dat”); // !may not connect! fout.open(“output.dat”);

  • File may not exist
  • File may be misspelled
  • Perhaps wrong directory
  • etc
slide-7
SLIDE 7

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);

slide-8
SLIDE 8

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);

slide-9
SLIDE 9

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);

slide-10
SLIDE 10

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);

slide-11
SLIDE 11

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);

slide-12
SLIDE 12

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);

slide-13
SLIDE 13

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);

slide-14
SLIDE 14

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);

slide-15
SLIDE 15

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);

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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();

slide-22
SLIDE 22

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 12 Hurson Ali 41 Buechler Matt 87

slide-23
SLIDE 23

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 12 Hurson Ali 41 Buechler Matt 87

slide-24
SLIDE 24

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 12 Hurson Ali 41 Buechler Matt 87

slide-25
SLIDE 25

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 12 Hurson Ali 41 Buechler Matt 87

slide-26
SLIDE 26

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 12 Hurson Ali 41 Buechler Matt 87

slide-27
SLIDE 27

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 12 Hurson Ali 41 Buechler Matt 87

slide-28
SLIDE 28

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 12 Hurson Ali 41 Buechler Matt 87

slide-29
SLIDE 29

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.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

slide-30
SLIDE 30

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.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

slide-31
SLIDE 31

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.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

slide-32
SLIDE 32

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.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

slide-33
SLIDE 33

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.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

slide-34
SLIDE 34

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, Clayton Hurson, Ali Buechler, Matt Van Horn, Keith Walker, Betty Sue

slide-35
SLIDE 35

http://cs.mst.edu

End of Session