8 File Handling

Object Oriented Programming · Unit 8 · 6 hrs

File handling

Exam-focused notes for File handling (Object Oriented Programming, CSC166): what the TU syllabus asks and how it has actually been tested, with 8 solved past questions from this unit.

What this unit covers

  • Stream Class Hierarchy for Console Input/Output
  • Unformatted Input/Output
  • Formatted Input/Output with ios Member functions
  • Formatting with Manipulators
  • Stream Operator Overloading
  • File Input/output with Streams
  • Opening and Closing files
  • Read/Write from File
  • File Access Pointers and their Manipulators
  • Sequential and Random Access to File
  • Testing Errors during File Operations

Stream Class Hierarchy for Console Input/Output

20805 marks

What do you mean by stream? Explain different stream class for file input/output. [5]

A stream is a sequence of bytes flowing between a program and an input/output device (such as a file, keyboard, or screen). It acts as an abstraction layer between the program and the actual device. - We have been using the iostream standard library, which ...

Full solved answer →
207510 marks

Briefly explain the hierarchy of stream classes. Write a program that overloads extraction and insertion operators.[10]

C++ provides a rich set of stream classes for performing input/output operations. These classes are organized in a hierarchy, all defined in the header files <iostream, <fstream, and <sstream. Class Header Description ---------------------------- ios <ios B...

Full solved answer →

File Access Pointers and their Manipulators

20795 marks

Write a program to illustrate the use of seekg() and tellg(). [5]

- tellg() -- Returns the current get pointer position (current read position) in the file as a number of bytes from the beginning. - seekg() -- Moves the get pointer to a specified position in the file so that the next read operation starts from that positi...

Full solved answer →

Unformatted Input/Output

20785 marks

What is the use of get and getline functions? Explain with suitable example. [5]

Both get() and getline() are input functions in C++ used to read character(s) or strings from the input stream (cin). They are particularly useful when reading input that includes whitespace characters (spaces, tabs, newlines), which the standard operator c...

Full solved answer →
20755 marks

How is character I/O different from Binary I/O? Explain with examples. [5]

--- Character I/O (also called Text Mode I/O) deals with data as a sequence of human-readable characters. Data is stored and read in ASCII/text format. - Uses functions like get(), put(), getline(), , << - Data is converted to/from text representation - New...

Full solved answer →

File Input/output with Streams

20785 marks

What is meant by stream? Write a program that reads content of file data.txt and displays the content in monitor. [5]

A stream is a sequence of bytes flowing between a program and an input/output device (such as a keyboard, monitor, or file). In C++, a stream acts as an interface between the program and the actual I/O device, allowing data to flow in or out. We have been u...

Full solved answer →
2080.15 marks

Write a program that stores information of employees in a file and displays the file's content in ascending order of their salary. [5]

Ofstream is used to write information to a file, and ifstream is used to read information from a file. The header <fstream must be included. After reading all employee records back from the file, we sort them by salary in ascending order before displaying. ...

Full solved answer →

Read/Write from File

20765 marks

Which function can be used for reading and writing object? Define briefly. Write a program that reads values of two objects of students class (assume data members are id, name, and level) and display the data in monitor. [5]

In C++, the read() and write() member functions of file stream classes are used for reading and writing objects (binary data) to/from files or streams. Used to write an object (block of bytes) to a stream. Syntax: - (char ) casts the object address to a cha...

Full solved answer →