• No results found

CSL 113 Lab11

N/A
N/A
Protected

Academic year: 2020

Share "CSL 113 Lab11"

Copied!
10
0
0

Loading.... (view fulltext now)

Full text

(1)

Semester 02 (Spring 2016) Instructor: Ms. Fasiha /Ms. Sumeera Lab Engineer: Mr. M. Talha/Mr.TMarouf

Lab 11: I/O stream File handling

Objective(s) : Upon completion of this lab session, learners will be able to: 1. Able to read from a file

2. Able to write in a file

Streams

As we have learned in previous lectures that input/output in C++ is based on Streams. A stream is an object that enables the flow of data between a program and some I/O device or file

– If the data flows into a program, then the stream is called an input stream

– If the data flows out of a program, then the stream is called an output stream

In C++ we have two types of streams.

(2)

File Streams : Standard streams enable to flow of data between a program to Files stored on secondary storage of computer. File streams are created, connected to files, and disconnected from files by the programmer.

To use file streams, we need to create stream variables and to associate those stream variables to a file. There are certain steps every programming must follow.

Creating file streams

1. First define stream objects

 ifstream [stream variable name]; // to read from file  ofstream [stream variable name]; // to write in file

 fstream [stream variable name]; // for both (reading and writing)

2. Connecting file streams.

 Open ()

3. Disconnecting file streams

 Close ()

Important File Functions

Input related functions

ifstream fsin;

 fsIn.open(const char[] fname)

connects stream fsIn to the external file fname.

 fsIn.get(char& character)

extracts next character from the input stream fsIn and places it in the character variable character.

(3)

tests for the end-of-file condition.

Output related functions

ofstream fsOut;

 fsOut.open(const char[] fname)

connects stream fsOut to the external file fname.

 fsOut.put(char character)

inserts character character to the output stream fsOut.

 fsOut.eof()

tests for the end-of-file condition.

File Open Modes (Flags)

Name

Description

ios::in

Open file to read

ios::out

Open file to write

ios::app

All the data you write, is put at the end of the file.

It calls ios::out

ios::ate

All the data you write, is put at the end of the file. It does not

call ios::out

ios::trunc

Deletes all previous content in the file. (empties the file)

ios::nocreate

If the file does not exists, opening it with the open() function

gets impossible.

ios::noreplace If the file exists, trying to open it with the open() function,

returns an error.

(4)

Example – Reading from a File

# Output 10 30 50 60 20 40 60 80 30 50 70 90 40 60 80 100 50 70 90 110 60 80 100 120

10 30 50 6050 70 90 1100

Example – Writing to a File

(5)
(6)

EXERCISES

Exercise 1

Write a C++ that perfrom following task:

 Create a function Student() that prompt user to a student record. Student contains following details.

ID, Full Name, Email, Department and Phone Number.

 Store the input in file student.txt

 In main program call function Student()

 Program should prompt user that you want to enter a new record. If ‘Y’ ask user for new details. If ‘N’, program should terminate. (Use event controlled loop)

#Sample Output Enter new record:

---Full Name: Sarmad Soomro

ID: 11734

Email: [email protected] Department: Computer Science Phone Number: 090078601

Do you want to enter new record(Y/N)? ___

Exercise 2

Refer to you Exercise 2.

(7)

Exercise 3

Refere to you exercise 1 and 2.

Read the data from file student.txt (line by line) and print on you computer screen as follows:

#Sample Output

Student Records Saved:

Sarmad Soomro 11743 [email protected] Computer Science 090078601 Majid Ilim 11603 [email protected] Managment Sciences 090078601 Asim Rizvi 11700 [email protected] Computer Science 090078601

Exercise 4

Write a C++ program that compute Net Salary of Employee. Program contains two user defined functions empSalary() and display().

 Create a structure of Employee that contains following data members:

o EmployeeNumber, Name, BasicSalary, HouseAllowence, MedicalAllowence,Tax, GrossPay and NetSalary

empSalary() compute salary with given criteria Salary will be computer on following criteria

o HouseAllowence = 10% of BasicSalary

o Medical Allowence = 5% of Basic Salary

o Tax = 4 % of Basic Salary

o GrossSalary = Basic+HouseAllowence+MedicalAllowence

o NetSalary = GrossSalary – Tax

(8)

Exercise 5

Write a C++ program for billing system of MovInPeak restaurant. The program should perform following tasks:

 Show menu to customer for orders.

 Allow the customer to select more than one item from the menu.

 Calculate and print the bill.

Assume that the restaurant offers the following items (the price of each item is shown to the right of the item):

 Omlet $1.45

 French Omlet $2.45

 Muffin $0.99

 French Toast $1.99

 Fruit Basket $2.49

 Cereal $0.69

 Coffee $0.75

 Tea $0.50

Notes:

 Store these prices in a file price.txt by using notepad. (No need to do in DevC++)

 Your program must contain following functions:

o Function showMenu: This function shows the different items offered by the restaurant and tells the user how to select the items. Read the items from file price.txt and display to user.

o Function printCheck: This function calculates, print the receipt to user and also store record in a file moveinpeak.txt.

(9)

#Sample Output

Welcome to MovinPeak Restaurant

What do you want to order. Please enter quantity of each item: Item –--- Price

----Omlet $1.45 : 0

French Omlet $2.45 : 1

Muffin $0.99 : 1

French Toast $1.99 : 0 Fruit Basket $2.49 : 0 Cereal $0.69 : 0 Coffee $0.75 : 1 Tea $0.50 : 0 ************************

Bill

************************ French Omlel $2.45

Muffin $0.99

Coffee $0.75

Tax $0.20

************************ Amount Due $4.39

************************

(10)

References

Related documents

• A list of bed bug resources. Once prepared, the pamphlet will be subject to a non-binding 60-day public comment period before distribution. After publication, landlords would

The lack of archaeological evidence has shifted academic interest to the texts themselves, which however do not provide a clear distinction between the Chorus’ level of action

(dishonest whore), so she teaches her that if a boy really likes you, he'll: take you out (spend money on you); date you exclusively (he's willing to let you train him, and he

According to the vintage model approach, the capital-output ratio for Hungary is about one- half of the US ratio, and two-thirds of the UK ratio (see Chart 4). If we accept

United States: Approved and/or distributed by Deutsche Bank Securities Incorporated, a member of FINRA, NFA and SIPC. Analysts employed by non-US affiliates may not be

The regularization method of Horn and Schunck is used to create schlieren images using two data sets: a supersonic jet plume/shock interaction from the NASA Ames Unitary Plan

95% CI: 95% Confidence Interval; ANCOVA: Analysis of Covariance;; BIMF: Barkin Index of Maternal Functionning; EPDS: Edinburgh Postpartum Depression Scale; ICC: Intra

Armed with a simple magnet, intruders can defeat reed switch magnetic contacts, bypass the security system, and enter almost any home undetected.. Reed switch