1 / 29

File Input & Output Lesson xx

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

leiko
Download Presentation

File Input & Output Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File Input & OutputLesson xx

  2. Objectives • Data Streams • Numeric Output • Numeric Input • Multiple Numeric Output • Multiple Numeric Input • Character Output • Character Input • String Output • String Input • Current Position Pointer

  3. Streams • Input and output is accomplished via streams in C++. • A stream is a flow (movement) of data

  4. Console I/O int a; cin >> a; cout << a;

  5. Defining Other Streams //file I/O int a; fin >> a; fout << a; //console I/O int a; cin >> a; cout << a;

  6. Numeric Output #include <fstream> using std::ofstream; int main() { ofstreamfout; fout.open("junk.dat"); int a = 17; fout << a; fout.close();   return 0; }

  7. File Setup #include <fstream> using std::ofstream; int main() { ofstreamfout; fout.open("junk.dat"); int a = 17; fout << a; fout.close();   return 0; }

  8. 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; }

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

  10. Numeric Input #include <fstream> using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a;   fin >> a; fin.close();   return 0; }

  11. ifstream Object #include <fstream> using std::ifstream; int main() { ifstream fin; fin.open("junk.dat"); int a;   fin >> a; fin.close();   return 0; }

  12. 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; }

  13. 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; }

  14. endl 10 20 30 40 50 60 70 80 90 100 102030405060708090100 fout << i << endl; fout << i ;

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

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

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

  18. 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; }

  19. 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; }

  20. String Output Part 1 #include <fstream> using std::ofstream; using std::ios; #include <iostream> using std::cin; using std::endl; #include <string>

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

  22. 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; }

  23. 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; }

  24. 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; }

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

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

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

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

  29. Summary • Data Stream • Numeric Output • Numeric Input • Multiple Numeric Output • Multiple Numeric Input • Character Output • Character Input • String Output • String Input • Current Position Pointer

More Related