Transcript Slide 1











Data Streams
Numeric Output
Numeric Input
Multiple Numeric Output
Multiple Numeric Input
Character Output
Character Input
String Output
String Input
Current Position Pointer
• Input and output is accomplished
via streams in C++.
•A stream is a flow (movement) of
data
int a;
cin >> a;
cout << a;
//console I/O
//file I/O
int a;
cin >> a;
int a;
fin >> a;
cout << a;
fout << a;
#include <fstream>
using std::ofstream;
int main()
{
ofstream fout;
fout.open("junk.dat");
int a = 17;
fout << a;
fout.close();
return 0;
}
#include <fstream>
using std::ofstream;
int main()
{
ofstream fout;
fout.open("junk.dat");
int a = 17;
fout << a;
fout.close();
return 0;
}
#include <fstream>
using std::ofstream;
int main()
{
ofstream fout;
fout.open("junk.dat");
int a = 17;
fout << a;
fout.close();
return 0;
}
1.
2.
3.
4.
5.
Include the correct header files
Set up a stream object
Attach a file to the stream object
Write to file using the stream object
Close file
#include <fstream>
using std::ifstream;
int main()
{
ifstream fin;
fin.open("junk.dat");
int a;
fin >> a;
fin.close();
return 0;
}
#include <fstream>
using std::ifstream;
int main()
{
ifstream fin;
fin.open("junk.dat");
int a;
fin >> a;
fin.close();
return 0;
}
#include <fstream>
using std::ifstream;
int main()
{
ifstream fin;
fin.open("junk.dat");
int a;
fin >> a;
fin.close();
return 0;
}
#include <fstream>
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;
}
10
20
30
40
50
60
70
80
90
100
fout << i << endl;
102030405060708090100
fout << i ;
junk.dat
#include <fstream>
using std::ifstream;
#include <iostream>
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
#include <fstream>
using std::ifstream;
#include <iostream>
using std::cout;
int main()
{
ifstream fin;
fin.open("junk.dat");
int a;
while (fin >> a)
cout << a << endl ;
fin.close();
return 0;
}
junk.dat
10
20
30
40
50
60
70
80
90
100
#include <string>
#include <fstream>
using std::ofstream;
#include <iostream>
using std::cin;
int main()
{
char ch;
ofstream fout;
fout.open("test.dat");
while ( cin.get(ch) && (ch != '\n') )
fout.put(ch);
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;
}
#include <fstream>
using std::ifstream;
#include <iostream>
using std::cout;
int main()
{
char ch;
ifstream fin;
fin.open("test.dat");
while(fin.get(ch))
cout << ch;
fin.close();
return 0;
}
#include <fstream>
using std::ofstream;
using std::ios;
#include <iostream>
using std::cin;
using std::endl;
#include <string>
int main()
{
char buf[80];
ofstream fout;
fout.open("test.dat");
while(cin.getline(buf, 80) &&(strlen(buf) > 0))
fout << buf << endl;
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;
}
#include <fstream>
using std::ifstream;
#include <iostream>
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;
}
#include <fstream>
using std::ifstream;
#include <iostream>
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;
}
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
junk.dat
CP
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
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
ifstream fin;
fin.open("junk.dat");
int a;
while (fin >> a)
cout << a << endl ;
CP
10
20
30
40
50
60
70
80
90
100
• Data Stream
• Numeric Output
• Numeric Input
• Multiple Numeric Output
• Multiple Numeric Input
• Character Output
• Character Input
• String Output
• String Input
•Current Position Pointer