Cs 1114 - lecture-29


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Cs 1114 - lecture-29

  1. 1. Programming Fundamentals
  2. 2. Input and Output Programming Fundamentals | Lecture-29 RAM Program Data
  3. 3. What is a Stream? <ul><li>In Dictionary </li></ul><ul><ul><li>Noun: A flow of water in a channel ( ندی ) </li></ul></ul><ul><ul><li>Verb: To come or go in large numbers </li></ul></ul><ul><li>In Programming Languages </li></ul><ul><ul><li>An abstract representation of “input data source” or “output data destination” </li></ul></ul><ul><ul><ul><li>A stream hides all the underlying details of an input source or output destination from a Program </li></ul></ul></ul><ul><ul><ul><ul><li>The program treats it like an actual input data source or output data destination </li></ul></ul></ul></ul><ul><ul><li>Example: cin and cout </li></ul></ul>Programming Fundamentals | Lecture-29
  4. 4. Input and Output Programming Fundamentals | Lecture-29 RAM Program Data stream stream stream stream
  5. 5. “ Stream” Concept Advantage <ul><li>A program handles all kinds of streams in exactly the same way </li></ul><ul><ul><li>For Input, whether a stream is opened from keyboard or from a file or from mic </li></ul></ul><ul><ul><ul><li>The program is only concerned with data which it will extract from the stream </li></ul></ul></ul><ul><ul><li>For output, whether a stream is opened from monitor, file, or speaker </li></ul></ul><ul><ul><ul><li>The program is only concerned with data which it will insert into the stream to be outputted </li></ul></ul></ul>Programming Fundamentals | Lecture-29
  6. 6. Working with Streams <ul><li>Steps to perform </li></ul><ul><ul><li>Create a Stream </li></ul></ul><ul><ul><li>Connect (or open) it to an input source or output destination </li></ul></ul><ul><ul><li>Disconnect (or close) it when you are done </li></ul></ul><ul><li>Characteristics of a Stream </li></ul><ul><ul><ul><li>Input / Output Mode </li></ul></ul></ul><ul><ul><ul><ul><ul><li>Input only, Output Only or both </li></ul></ul></ul></ul></ul><ul><ul><ul><li>Input or Output Entity it can connect to </li></ul></ul></ul><ul><ul><ul><li>Transfer mode </li></ul></ul></ul><ul><ul><ul><ul><ul><li>Text or binary </li></ul></ul></ul></ul></ul>Programming Fundamentals | Lecture-29
  7. 7. Standard Streams <ul><li>You’ve already been working with two standard streams </li></ul><ul><ul><li>Console Input: named as cin </li></ul></ul><ul><ul><li>Console Output: named as cout </li></ul></ul><ul><li>Did you perform required steps for these streams? </li></ul><ul><ul><li>No, because when you include <iostream> in your program, they are done automatically </li></ul></ul><ul><li>Two other standard streams </li></ul><ul><ul><li>Console Error (cerr) and Console Log (clog) </li></ul></ul>Programming Fundamentals | Lecture-29
  8. 8. Operators for Streams <ul><li>Stream Extraction Operator (>>) </li></ul><ul><ul><li>Which extracts input data from the stream and save it in program memory </li></ul></ul><ul><ul><ul><li>Stream Name >> Variable Name </li></ul></ul></ul><ul><ul><ul><ul><li>Example: cin >> number; </li></ul></ul></ul></ul><ul><li>Stream Insertion Operator (<<) </li></ul><ul><ul><li>Which picks data from program memory and inserts it to a stream to be outputted </li></ul></ul><ul><ul><ul><li>Stream Name << Variable Name </li></ul></ul></ul><ul><ul><ul><ul><li>Example: cout << number; </li></ul></ul></ul></ul>Programming Fundamentals | Lecture-29
  9. 9. What is a File? <ul><li>An external collection of related data treated as a unit (stored with a single name) </li></ul><ul><li>Purpose </li></ul><ul><ul><li>To keep record of data permanently </li></ul></ul><ul><ul><ul><li>Contents of Primary Memory (RAM) are not permanent </li></ul></ul></ul><ul><ul><ul><li>Variables declared in a program are destroyed when program terminates </li></ul></ul></ul><ul><ul><ul><li>Collection of data is often too large to reside entirely in main memory at one time </li></ul></ul></ul>Programming Fundamentals | Lecture-29
  10. 10. Types of Files <ul><li>Text (Human readable) </li></ul><ul><ul><li>Everything is stored as characters whether it is an integer or float value </li></ul></ul><ul><ul><li>Each line of data ends with a newline character </li></ul></ul><ul><ul><li>There is a special character called end-of-file (EOF) at the end of file. </li></ul></ul><ul><li>Binary (Not human readable) </li></ul><ul><ul><li>Data is stored in exactly the same format as it is stored in memory </li></ul></ul><ul><li>We’ll work with text (sequential) files in this course </li></ul>Programming Fundamentals | Lecture-29
  11. 11. Working with Files <ul><li>To treat file as an input source or as an output destination in our program </li></ul><ul><ul><li>We just need a stream because our program can conveniently work with streams </li></ul></ul><ul><li>We’ll need to perform the necessary steps this time because file streams are not standard streams i.e,. </li></ul><ul><ul><li>Create a stream </li></ul></ul><ul><ul><li>Open it </li></ul></ul><ul><ul><li>Close it finally </li></ul></ul>Programming Fundamentals | Lecture-29
  12. 12. File Streams <ul><li>Three types </li></ul><ul><ul><li>Input only – ifstream </li></ul></ul><ul><ul><li>Output only – ofstream </li></ul></ul><ul><ul><li>Both input and output – fstream </li></ul></ul><ul><li>To use them we need to include <fstream> </li></ul>Programming Fundamentals | Lecture-29
  13. 13. 1. Creating File Stream <ul><li>Syntax </li></ul><ul><ul><li>TypeName<space>identifier </li></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><ul><li>ifstream fInputStream; </li></ul></ul></ul><ul><ul><ul><li>ofstream fOutputStream; </li></ul></ul></ul><ul><ul><ul><li>fstream fInOutStream; </li></ul></ul></ul>Programming Fundamentals | Lecture-29
  14. 14. 2. Connecting Stream to File <ul><li>Syntax </li></ul><ul><ul><li>Identifier.open(“filePath”); </li></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><ul><li>fInputStream.open(“abc.txt”); </li></ul></ul></ul><ul><ul><ul><ul><li>// file abc.txt in the same folder </li></ul></ul></ul></ul><ul><ul><ul><li>fOutputStream.open(“C:xyz.txt”); </li></ul></ul></ul><ul><ul><ul><ul><li>// complete path of the file </li></ul></ul></ul></ul><ul><ul><ul><li>fInOutStream.open(“abc.txt”); </li></ul></ul></ul>Programming Fundamentals | Lecture-29
  15. 15. File Input/Output <ul><li>When a stream is created and connected to a file for output, it is same like console output stream for a program </li></ul><ul><ul><li>fOutputStream << aString; </li></ul></ul><ul><ul><li>fInputOutputStream << aString; </li></ul></ul><ul><li>When a stream is created and connected to a file for input, it is same like console input stream for a program </li></ul><ul><ul><li>fInputStream >> aString; </li></ul></ul><ul><ul><li>fInputOutputStream >> aString; </li></ul></ul>Programming Fundamentals | Lecture-29
  16. 16. File Marker <ul><li>An indicator in a file stream that </li></ul><ul><ul><li>Determines the position from which data should be read or to which data should be written in a file </li></ul></ul><ul><li>When a file is opened for input or output, it is always positioned at the beginning of the file </li></ul>Programming Fundamentals | Lecture-29
  17. 17. Handling Errors <ul><li>Ensure the file stream is opened before proceeding further </li></ul><ul><li>Ensure the file stream is properly closed so system resources are freed up </li></ul><ul><li>See FileDemo2.cpp </li></ul>Programming Fundamentals | Lecture-29
  18. 18. Structure of Data in Files <ul><li>C++ imposes no structure on a file </li></ul><ul><ul><li>You can put data in a file the way you want </li></ul></ul><ul><ul><li>The concept of a record doesn’t not exist </li></ul></ul><ul><li>The programmer must structure files to meet the application's requirements </li></ul><ul><ul><li>For example, you can save each record on a separate line in a text file and can use “separator character” to separate different data items within a record </li></ul></ul><ul><li>See RecordsInFile.cpp </li></ul>Programming Fundamentals | Lecture-29
  19. 19. Updating a Text File <ul><li>The process is not straightforward, as file marker is always positioned at the beginning when you reopen a file </li></ul><ul><ul><li>Either to add another record in it or to update an existing one </li></ul></ul><ul><ul><li>There are functions to change to the position of File Marker but moving it to a right position is not possible in text files </li></ul></ul>Programming Fundamentals | Lecture-29
  20. 20. How to Update? <ul><li>Read all the records in program memory </li></ul><ul><li>Add or update </li></ul><ul><li>Save all the existing along with new or updated ones in a temporary file </li></ul><ul><li>Delete the old file </li></ul><ul><li>Rename temporary file as the old one </li></ul>Programming Fundamentals | Lecture-29
  21. 21. Task (to be done by next lecture) <ul><li>After writing records to a file, read all records from the file and display them where each item in a record should be separately output </li></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>For each record (read from file), show like that </li></ul></ul></ul><ul><ul><ul><ul><li>Account No: </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Name: </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Balance: </li></ul></ul></ul></ul>Programming Fundamentals | Lecture-29