Lec 49 - stream-files

  • 204 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
204
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
12
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Student Book
  • Used tootsie roll pieces as data bytes and a large tootsie roll as a line of data on a cardboard card and drawing on the chalkboard.
  • Used tootsie roll pieces as data bytes and a large tootsie roll as a line of data on a cardboard card and drawing on the chalkboard.

Transcript

  • 1. Chapter: 12 Stream and Files Lecture: 49 Date: 13.11.2012
  • 2. Objectives Overview of stream classes Showing how to perform file-related activities using streams:  How to read and write data to files in a variety of ways  How to handle files or I/O errors  How files and OOP are related
  • 3. Streams Stream (flow of data)  A transfer of information in the form of a sequence of bytes In C++, a stream is represented by an object of a particular class; e.g., cin and cout are objects of iostream class. I/O Operations:  Input: A stream that flows from an input device ( i.e.: keyboard, disk drive, network connection) to main memory  Output: A stream that flows from main memory to an output device ( i.e.: screen, printer, disk drive, network connection)
  • 4. keyboard standard input stream CPU standard output MEMmonitor streamterminalconsole HDDWhat information travels across? Streams
  • 5. keyboard standard input stream CPU standard output MEMmonitor streamterminal fileconsole input stream LOAD HDD What information READ travels across? file files output Streams stream SAVE WRITE
  • 6. Stream Class Hierarchy
  • 7. What is a File? File: A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused. File Names: All files are assigned a name that is used for identification purposes by the operating system and the user.
  • 8. File Names and ExtensionsFile Name and File ContentsExtensionM Y P R O G .B A S BASIC programM E N U .B A T DOS Batch FileIN S T A L L .D O C Documentation FileC R U N C H .E X E Executable FileB O B .H T M L HTML (Hypertext Markup Language) File3 D M O D E L .J A V A Java program or appletIN V E N T .O B J Object FileP R O G 1 .P R J Borland C++ Project FileA N S I.S Y S System Device DriverR E A D M E .T X T Text File
  • 9. Writing to a FileXYZ
  • 10. Reading From a FileXYZ
  • 11. Disk File I/O with Streams Most programs need to save data to disk files and read it back in. Working with disk files requires an other set of classes:  ifstream for (file) input  fstream for both (file) input and output  ofstream for (file) output Objects of these classes can be associated with disk files, and their member functions can be used to read and write to files.
  • 12. The Process of Using a File Using a file in a program is a simple three-step process 1) The file must be opened. If the file does not yet exits, opening it means creating it. 2) Information is then saved to the file, read from the file, or both. 3) When the program is finished using the file, the file must be closed.
  • 13. #include <iostream>#include <fstream>#include <conio.h> Writing to Fileusing namespace std;int main(){ char ch = ‘x’; int j = 77; double d = 6.02; string str1 = “Book”; //strings without embedded spaces string str2 = “Pen”;ofstream outfile(“fdata.txt”); //create ofstream objectoutfile << ch //insert (write) data << j << ‘ ‘ //needs space between numbers << d << str1 << ‘ ‘ //needs spaces between strings << str2;cout << “File writtenn”;getch(); return 0; }
  • 14. #include <iostream>#include <fstream>#include <conio.h> Reading from Fileusing namespace std;int main(){ char ch; int j; double d; string str1; string str2;ifstream infile("fdata.txt");//create ifstream object //extract (read) data from itinfile >> ch >> j >> d >> str1 >> str2;cout << ch << endl //display the data << j << endl << d << endl << str1 << endl << str2 << endl; getch(); return 0; }
  • 15. Writing to File (easy way)#include <iostream>#include <fstream>#include <conio.h>using namespace std;int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.n"; myfile << "This is another line.n"; myfile.close(); } else cout << "Unable to open file";getch();return 0; }
  • 16. Reading from File (easy way)#include <iostream>#include <fstream>#include <conio.h>using namespace std;int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile, line); //extract characters into object myfile until line cout << line << endl; } myfile.close(); } else cout << "Unable to open file";getch(); return 0; }