290 likes | 437 Views
File Input & Output Lesson xx. Objectives. Data Streams Numeric Output Numeric Input Multiple Numeric Output Multiple Numeric Input Character Output Character Input String Output String Input Current Position Pointer. Streams. Input and output is accomplished via streams in C++.
E N D
Objectives • Data Streams • Numeric Output • Numeric Input • Multiple Numeric Output • Multiple Numeric Input • Character Output • Character Input • String Output • String Input • Current Position Pointer
Streams • Input and output is accomplished via streams in C++. • A stream is a flow (movement) of data
Console I/O int a; cin >> a; cout << a;
Defining Other Streams //file I/O int a; fin >> a; fout << a; //console I/O int a; cin >> a; cout << a;
Numeric Output #include <fstream> using std::ofstream; int main() { ofstreamfout; fout.open("junk.dat"); int a = 17; fout << a; fout.close(); return 0; }
File Setup #include <fstream> using std::ofstream; int main() { ofstreamfout; fout.open("junk.dat"); int a = 17; fout << a; fout.close(); return 0; }
Writing Data to File #include <fstream> using std::ofstream; int main() { ofstreamfout; fout.open("junk.dat"); int a = 17; fout << a; fout.close(); return 0; }
Steps for Writing to a File 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
Numeric Input #include <fstream> using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a; fin >> a; fin.close(); return 0; }
ifstream Object #include <fstream> using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a; fin >> a; fin.close(); return 0; }
Read Number from File #include <fstream> using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a; fin >> a; fin.close(); return 0; }
Multiple Numeric Output #include <fstream> using std::ofstream; using std::endl; int main() { ofstreamfout; fout.open("junk.dat"); inti; for (i = 10; i < 110; i += 10) fout << i << endl; fout.close(); return 0; }
endl 10 20 30 40 50 60 70 80 90 100 102030405060708090100 fout << i << endl; fout << i ;
Multiple Numeric Input 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
Multiple Numeric Input 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
Character Output Part 1 #include <string> #include <fstream> using std::ofstream; #include <iostream> using std::cin; int main() { char ch; ofstreamfout; fout.open("test.dat"); while ( cin.get(ch) && (ch != '\n') ) fout.put(ch);
Character Output Part 2 char buf[] = "Feed a child fish & he eats for one day. Teach him to fish and he eats forever."; inti; for (i = 0; i < (int)strlen(buf); ++i) fout.put(buf[i]); fout.close(); return 0; }
Character Input #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; }
String Output Part 1 #include <fstream> using std::ofstream; using std::ios; #include <iostream> using std::cin; using std::endl; #include <string>
String Output Part 2 • int main() { char buf[80]; ofstreamfout; fout.open("test.dat"); • while(cin.getline(buf, 80) &&(strlen(buf) > 0)) fout << buf << endl;
String Output Part 3 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; }
String Input #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; }
String Input #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; }
File Processing Commands 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
Current Position Pointer (CP) 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
Current Position Pointer (CP) 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
Current Position Pointer (CP) junk.dat ifstream fin; fin.open("junk.dat"); int a; while (fin >> a) cout << a << endl ; 10 20 30 40 50 60 70 80 90 100 CP
Summary • Data Stream • Numeric Output • Numeric Input • Multiple Numeric Output • Multiple Numeric Input • Character Output • Character Input • String Output • String Input • Current Position Pointer