Class 12 Computer Science Notes for KPK Chapter 9 File Handling

Class 12 Computer Science Notes for KPK Chapter 9 File Handling

KPK Grade 12 Computer Science

Class 12 Computer Science Notes for KPK Chapter 9 File Handling

Answer:
FILES
Definition:

The combination of characters, words and records are called files.
Explanation:
The operations on these files are the major focus point of file handling. Suppose we have a file on the disk and want to open it then reading from or writing into the file before closing it will be termed as handling files.
Types:
C++ divides files into two different types based on how they store data. These are:
- Text files
- Binary files

- Text Files:
Those files that store data in text format which is readable by humans.
Example:
The text file is the C++ source program which can be read by a human.

- Binary Files:
Those files which store data in binary format which is not readable by the humans but readable by the computers.
Binary files are directly processed by computers. Binary files are normally used to store large data files. They take less space to store data as compared to text files.
Example:
An integer having a length of six digits will take six bytes to accommodate in a text file while in binary files they will take only two bytes. The best example of a binary file is the object file of a C++ source file that is generated by the compiler of C++

Q.4) Define file. Describe different types of file.

Answer:
FILES
Definition:

The combination of characters, words and records are called files.
Explanation:
The operations on these files are the major focus point of file handling. Suppose we have a file on the disk and want to open it then reading from or writing into the file before closing it will be termed as handling files.
Types:
C++ divides files into two different types based on how they store data. These are:
- Text files
- Binary files

- Text Files:
Those files that store data in text format which is readable by humans.
Example:
The text file is the C++ source program which can be read by a human.

- Binary Files:
Those files which store data in binary format which is not readable by the humans but readable by the computers.
Binary files are directly processed by computers. Binary files are normally used to store large data files. They take less space to store data as compared to text files.
Example:
An integer having a length of six digits will take six bytes to accommodate in a text file while in binary files they will take only two bytes. The best example of a binary file is the object file of a C++ source file that is generated by the compiler of C++

Q.6) Define streams. Describe the input and output stream in detail.

Answer:
STREAM
Definition:

“A stream can be thought of as a sequence of bytes of infinite length that is used as a buffer to hold data to be processed.”
In C++ a stream is a sequence of bytes associated with a file. Most of the times streams are used to assure a good and secure flow of data between an application and a file.

Types:
In C++ there are two types of streams:
- Input Stream
- Output Stream
Input Stream takes any sequence of bytes from an input device such as keyboard, a file or a network, while output streams are used to hold output for a particular data consumer, such as a monitor, a file, or a printer. When writing data to an output device, the device may not be ready to accept the data e.g. the printer may still be warming up when the program writes data to its output stream. The data will reside in the output stream until the printer starts consuming it.

Q.7) What is meant by the term mode of file opening? Describe different modes of opening file .

Answer:
MODE OF OPENING A FILE
While opening a file, we tell the compiler what we want to do with it i.e. we want to read the file or write into the file or want to modify it. In order to open a file in any desired mode the member function open ( ) should take mode as an argument along with the file name.
Syntax:
The general syntax is as follows:
open(filename, mode);
Explanation:
Here, file name representing the name of the file to be opened, and mode is an optional parameter with an combination of the following flags:

FLAGS FUNCTIONIos::inOpen for input operationsios::outOpen for output operationsios::binaryOpen in binary mode.ios::ateSet the initial position at the end of the file. If the flag is not set to any value, the initial position is the beginning of the file.Ios::appAll output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations.ios::trunk If the file opened for output operations already exists then its previous contents are deleted and replaced by the new one.

All these flags can be combined using the bitwise operator OR |.
For example, if we want to open the file test.bin in binary mode to add data we could do it by the following call to member function open( ).
Ofstream myfile;
myfile.open(“test.bin”, ios::out | ios::app | ios::binary);

Q.7) What is meant by the term mode of file opening? Describe different modes of opening file .

Answer:
MODE OF OPENING A FILE
While opening a file, we tell the compiler what we want to do with it i.e. we want to read the file or write into the file or want to modify it. In order to open a file in any desired mode the member function open ( ) should take mode as an argument along with the file name.
Syntax:
The general syntax is as follows:
open(filename, mode);
Explanation:
Here, file name representing the name of the file to be opened, and mode is an optional parameter with an combination of the following flags:

FLAGS FUNCTIONIos::inOpen for input operationsios::outOpen for output operationsios::binaryOpen in binary mode.ios::ateSet the initial position at the end of the file. If the flag is not set to any value, the initial position is the beginning of the file.Ios::appAll output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations.ios::trunk If the file opened for output operations already exists then its previous contents are deleted and replaced by the new one.

All these flags can be combined using the bitwise operator OR |.
For example, if we want to open the file test.bin in binary mode to add data we could do it by the following call to member function open( ).
Ofstream myfile;
myfile.open(“test.bin”, ios::out | ios::app | ios::binary);

Reated Post:

- 2nd-year computer science notes Chapter 1 Operating Systems

- 2nd-year computer science notes Chapter 2 System Development Life Cycle

- Computer Science Notes Chapter 5 Arrays and Strings

- Chapter 3 Object-Oriented Programming using C++

- 2nd year Computer science Notes Chapter 4 Control Structures

i am blogger.