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