IEO Level 2- English Olympiad (SOF) Class 9 Coaching Programs

⏳ 🎯 Online Tests (2 Tests [50 Questions Each]): NTA Pattern, Analytics & Explanations

Click Here to View & Get Complete Material

Rs. 200.00

3 Year Validity (Multiple Devices)

🎓 Study Material (303 Notes): 2024-2025 Syllabus

Click Here to View & Get Complete Material

Rs. 450.00

3 Year Validity (Multiple Devices)

🎯 250 MCQs (& PYQs) with Full Explanations (2024-2025 Exam)

Click Here to View & Get Complete Material

Rs. 200.00

3 Year Validity (Multiple Devices)

Files: Introduction, Objectives, Writing and Reading Data on the Data File, Opening a File

Illustration: Files: Introduction, Objectives, Writing and Reading Data on the Data File, Opening a File

Introduction

At times it is required to store data on hard disk or floppy disk in some application program. The data is stored in these devices using the concept of file.

Objectives

After going through this lesson, you would be able to:

  • store data in a file
  • access data record by record from the file
  • move pointer within the file
  • open or close file

File

A file is a collection of logically related records. A program usually requires two types of data communication.

Illustration: File

Writing Data on the Data File

The data flows from keyboard to memory and from memory to storage device.

keyboard memory hard disk/floppy disk

This is called output stream where stream is the flow of data and requires an of stream header file.

Reading Data from Data File

The data flows from storage device to memory and from memory to output device, particularly monitor.

datafile memory output device (screen)

external storage device (hard disk/floppy)

  • This is called input stream and requires if stream header file.
  • If both input stream and output stream are used in the same program, then header file fstream. h is required.
  • If header file fstream. h is included in the program, there is no need to include iostream. h explicitly.

Opening a File

A file can be opened in two ways:

(i) Using constructor function of a class.

(ii) Using the member function open () of the class.

  • Opening a file using constructor function.
  • The following statement opens the file STU. DAT in output mode, i.e.. , for writing data on the file.

of stream outfile ( “STU. DAT” ) ;

of stream is a class available in the compiler file.

outfile is any user defined object.

The statements

outfile << “TOTAL MARKS” ≪ “⧵n” ;

outfile << total ≪ “⧵n” ;

are used for writing data on the file. The newline character is used for moving the pointer to the next line.

Similarly, the following statement

if stream in file ( “STU. DAT” ) ;

opens the file “STU. DAT” in input mode, i.e.. , for reading purpose. The statements

in file ≫ string.

in file ≫ number.

read the data from the data file.