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;
//file I/O int a; fin >> a; fout << a; //console I/O int a; cin >> a; cout << a;
#include using std::ofstream; int main() { ofstream fout; fout.open("junk.dat"); int a = 17; fout << a; fout.close(); return 0; }
#include using std::ofstream; int main() { ofstream fout; fout.open("junk.dat"); int a = 17; fout << a; fout.close(); return 0; }
#include using std::ofstream; int main() { ofstream fout; fout.open("junk.dat"); int a = 17; fout << a; fout.close(); return 0; }
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
# include using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a; fin >> a; fin.close(); return 0; }
#include using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a; fin >> a; fin.close(); return 0; }
#include using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a; fin >> a; fin.close(); return 0; }
#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; }
fout << i << endl ; fout << i ;
#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; } junk.dat
#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; } junk.dat
#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);
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 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; }
#include using std::ofstream; using std::ios; #include using std::cin; using std::endl; #include
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 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; }
#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; }
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
ifstream fin; fin.open("junk.dat"); int a; while (fin >> a) cout << a << endl ; junk.dat CP
ifstream fin; fin.open("junk.dat"); int a; while (fin >> a) cout << a << endl ; junk.dat CP
ifstream fin; fin.open("junk.dat"); int a; while (fin >> a) cout << a << endl ; junk.dat CP
Data Stream Numeric Output Numeric Input Multiple Numeric Output Multiple Numeric Input Character Output Character Input String Output String Input Current Position Poi nter