Download presentation
Presentation is loading. Please wait.
Published byMuriel Richard Modified over 8 years ago
3
Data Streams Numeric Output Numeric Input Multiple Numeric Output Multiple Numeric Input Character Output Character Input String Output String Input Current Position Pointer
4
Input and output is accomplished via streams in C++. A stream is a flow (movement) of data
5
int a; cin >> a; cout << a;
6
//file I/O int a; fin >> a; fout << a; //console I/O int a; cin >> a; cout << a;
7
#include using std::ofstream; int main() { ofstream fout; fout.open("junk.dat"); int a = 17; fout << a; fout.close(); return 0; }
8
#include using std::ofstream; int main() { ofstream fout; fout.open("junk.dat"); int a = 17; fout << a; fout.close(); return 0; }
9
#include using std::ofstream; int main() { ofstream fout; fout.open("junk.dat"); int a = 17; fout << a; fout.close(); return 0; }
10
1. Include the correct header files 2. Set up a stream object 3. Attach a file to the stream object 4. Write to file using the stream object 5. Close file
11
# include using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a; fin >> a; fin.close(); return 0; }
12
#include using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a; fin >> a; fin.close(); return 0; }
13
#include using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a; fin >> a; fin.close(); return 0; }
14
#include using std::ofstream; using std::endl; int main() { ofstream fout; fout.open("junk.dat"); int i; for (i = 10; i < 110; i += 10) fout << i << endl; fout.close(); return 0; }
15
10 20 30 40 50 60 70 80 90 100 102030405060708090100 fout << i << endl ; fout << i ;
16
#include using std::ifstream; #include using std::cout; int main() { ifstream fin; fin.open("junk.dat"); int a; while (fin >> a) cout << a << endl ; fin.close(); return 0; } 10 20 30 40 50 60 70 80 90 100 junk.dat
17
#include using std::ifstream; #include using std::cout; int main() { ifstream fin; fin.open("junk.dat"); int a; while (fin >> a) cout << a << endl ; fin.close(); return 0; } 10 20 30 40 50 60 70 80 90 100 junk.dat
18
#include #include using std::ofstream; #include using std::cin; int main() { char ch; ofstream fout; fout.open("test.dat"); while ( cin.get(ch) && (ch != '\n') ) fout.put(ch);
19
char buf[] = "Feed a child fish & he eats for one day. Teach him to fish and he eats forever."; int i; for (i = 0; i < (int)strlen(buf); ++i) fout.put(buf[i]); fout.close(); return 0; }
20
#include using std::ifstream; #include using std::cout; int main() { char ch; ifstream fin; fin.open("test.dat"); while(fin.get(ch)) cout << ch; fin.close(); return 0; }
21
#include using std::ofstream; using std::ios; #include using std::cin; using std::endl; #include
22
int main() { char buf[80]; ofstream fout; fout.open("test.dat"); while(cin.getline(buf, 80) &&(strlen(buf) > 0)) fout << buf << endl;
23
fout << "Better to remain quiet\n"; fout << "and be thought a fool\n"; fout << "that to open one's mouth\n"; fout << "and erase all doubts.\n"; fout.close(); return 0; }
24
#include using std::ifstream; #include using std::cout; using std::endl; int main() { char buf[80]; ifstream fin; fin.open("test.dat"); while(fin.getline(buf, 80)) cout << buf << endl; fin.close(); return 0; }
25
#include using std::ifstream; #include using std::cout; using std::endl; int main() { char buf[80]; ifstream fin; fin.open("test.dat"); while(fin.getline(buf, 80)) cout << buf << endl; fin.close(); return 0; }
26
fout << x; //write x to file fin >> x; //read data from file into x fout.put(ch); //write character in ch onto file fin.get (ch); //read 1 character from file into ch fout << “Kow\n"; //write a string onto file fin.getline(b, 80); //read a string into array b
27
ifstream fin; fin.open("junk.dat"); int a; while (fin >> a) cout << a << endl ; 10 20 30 40 50 60 70 80 90 100 junk.dat CP
28
ifstream fin; fin.open("junk.dat"); int a; while (fin >> a) cout << a << endl ; 10 20 30 40 50 60 70 80 90 100 junk.dat CP
29
ifstream fin; fin.open("junk.dat"); int a; while (fin >> a) cout << a << endl ; 10 20 30 40 50 60 70 80 90 100 junk.dat CP
30
Data Stream Numeric Output Numeric Input Multiple Numeric Output Multiple Numeric Input Character Output Character Input String Output String Input Current Position Poi nter
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.