CBSE Class 12 Computer Science: Chapter 6 Data File Handling NCERT Solutions
This chapter focuses on Data File Handling in C++, specifically dealing with text files. Students will learn essential operations like opening, reading, and closing text files. The NCERT Solutions for Chapter 6 cover practical programming exercises designed to reinforce these concepts. Key topics include counting words, identifying words of a specific length, and counting occurrences of specific characters within a file. These solutions provide step-by-step guidance, helping students understand the logic behind file manipulation in C++ and prepare effectively for their examinations by practicing common file handling scenarios.
Quick info
| Board | CBSE |
|---|---|
| Class | Class 12 |
| Subject | Computer Science (C++) |
| Session | 2026 |
| Language | English |
| Type | NCERT Solutions |
| Chapter | Chapter 6 |
Chapter summary
Chapter 6 of the CBSE Class 12 Computer Science syllabus delves into Data File Handling, with a primary focus on text files. The NCERT Solutions provided here cover fundamental operations such as reading from and writing to text files. Exercises involve practical applications like counting words, finding words of a specific length, and tallying character frequencies, ensuring students grasp the practical aspects of file I/O in C++.
Learning outcomes
- Understand the process of opening and closing text files in C++.
- Implement functions to read data from text files.
- Develop programs to count words in a text file.
- Write code to count characters of specific types (alphabets) in a file.
- Apply string manipulation functions like strlen() for file processing.
Topics covered
Paper topics
- Data File Handling
- Text Files
- File Input/Output
- C++ File Streams
- ifstream
- fstream
- Reading from files
- Counting words in a file
- Counting characters in a file
- String length function
- Character comparison
- File handling functions
Important topics
- Text File Operations
- Reading Data using >>
- Reading Data using get()
- Counting words
- Counting specific characters
- File stream objects (ifstream, fstream)
PDF preview
Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.
Questions and Solutions
Question 1
Co-education system is necessary for a balanced society. With co-education system. Girls and Boys may develop a feeling of mutual respect towards each other.
The function should display the following: Total number of words present in the text file are: 24To count the words in a text file, we can read the file word by word and increment a counter for each word read. The stream extraction operator `>>` is suitable for this as it reads until whitespace.
Here is the C++ function:
#include <iostream>
#include <fstream>
#include <string>
void word_count() {
std::ifstream inputFile("opinion.txt");
std::string word;
int count = 0;
if (inputFile.is_open()) {
// Read words until the end of the file is reached
while (inputFile >> word) {
count++; // Increment count for each word read } inputFile.close();
std::cout << "Total number of words present in the text file are: " << count << std::endl;
} else {
std::cout << "Error opening file." << std::endl; } }
Explanation:
- The function `word_count` uses an `ifstream` object to open `"opinion.txt"`.
- A `std::string word` is used to store each word read from the file.
- The `while (inputFile >> word)` loop continues as long as words can be successfully extracted from the file.
- Inside the loop, `count` is incremented for every word read.
- Finally, the total count is displayed. Error handling for file opening is included.
Question 2
Example:
If the file contains: A boy is playing there. I love to eat pizza. A plane is in the sky. Then the output should be: 4
This function will read the file word by word and check the length of each word. If a word has exactly three letters, a counter is incremented. The final count is then displayed.
Here is the C++ function:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring> // For strlen
void countThreeLetterWords() {
std::ifstream inputFile("VOWEL.TXT");
char word[80];
int count = 0;
if (inputFile.is_open()) {
// Read words from the file
while (inputFile >> word) {
// Check if the length of the word is exactly 3
if (strlen(word) == 3) {
count++; } }
inputFile.close();
std::cout << "Number of three letter words = " << count << std::endl;
} else {
std::cout << "Error opening file." << std::endl; } }
Explanation:
- The function `countThreeLetterWords` opens `"VOWEL.TXT"` using `ifstream`.
- It reads words into a character array `word`.
- `strlen(word)` calculates the length of the current word.
- If the length is exactly 3, the `count` is incremented.
- After reading the entire file, the total `count` is printed.
Question 3
Example: If the file content is as follow: CBSE enhanced its CCE guidelines further. The AECount() function should display the output as A:1 E:7
This function reads the file character by character and checks if each character is 'A', 'a', 'E', or 'e'. Counters are maintained for 'A'/'a' and 'E'/'e' occurrences, and the final counts are displayed.
Here is the C++ function:
#include <iostream>
#include <fstream>
void AECount() {
std::fstream file("NOTES.TXT", std::ios::in);
char ch;
int countA = 0;
int countE = 0;
if (file.is_open()) {
// Read characters until the end of the file
while (file.get(ch)) {
// Check if the character is 'A' or 'a'
if (ch == 'A' || ch == 'a') {
countA++; } // Check if the character is 'E' or 'e'
else if (ch == 'E' || ch == 'e') {
countE++; } }
file.close();
std::cout << "A: " << countA << " E: " << countE << std::endl;
} else {
std::cout << "Error opening file." << std::endl; } }
Explanation:
- The function `AECount` uses an `fstream` object to open `"NOTES.TXT"` in input mode (`std::ios::in`).
- The `while (file.get(ch))` loop reads the file one character at a time.
- Inside the loop, `if` and `else if` statements check if the character `ch` matches 'A'/'a' or 'E'/'e'.
- Corresponding counters (`countA`, `countE`) are incremented.
- After processing all characters, the counts are displayed.
Common mistakes
- Incorrectly handling file opening and closing operations.
- Errors in loop conditions for reading file content until EOF.
- Misunderstanding how stream extraction (>>) reads words.
- Issues with character comparison, especially case sensitivity.
- Incorrectly using file stream methods like get() or >>.
Revision tips
- Practice each function by creating sample text files.
- Pay close attention to loop termination conditions when reading files.
- Understand the difference between reading character by character and word by word.
- Test your code with edge cases, like empty files or files with special characters.
- Review the use of `ifstream`, `ofstream`, and `fstream` objects.
Practice MCQs
Q1. Which header file is essential for file stream operations in C++?
Explanation: The `<fstream>` header file provides the necessary classes and functions for file input and output operations in C++.
Q2. What does the `!i.eof()` condition check in a file reading loop?
Explanation: The `eof()` function returns true if the end of the file has been reached. The `!i.eof()` condition ensures the loop continues as long as the end of the file has NOT been reached.
Q3. How does the stream extraction operator `>>` typically read data from a text file?
Explanation: The `>>` operator, when used with character arrays or strings, reads input until it encounters whitespace (space, tab, newline).
Q4. To count both uppercase 'A' and lowercase 'a', what condition should be used?
Explanation: The logical OR operator `||` is used to check if the character `x` is equal to either 'A' or 'a'.
Q5. Which function is used to read a single character from a file stream?
Explanation: The `get()` member function of file stream objects is used to read a single character from the file.
Frequently asked questions
What is the main purpose of Chapter 6 in CBSE Class 12 Computer Science?
Chapter 6 focuses on Data File Handling in C++, teaching students how to read from and write to text files, which is crucial for managing data persistently.
How do these NCERT Solutions help in understanding file handling?
The solutions provide rewritten, clear explanations and code examples for common file handling tasks like counting words and characters, making complex concepts easier to grasp.
Which C++ stream class is used for reading from files?
The `ifstream` class is primarily used for input operations, meaning reading data from files.
What is the difference between using `>>` and `get()` for reading from a file?
The `>>` operator reads data separated by whitespace, treating spaces, tabs, and newlines as delimiters. The `get()` function reads a single character at a time, including whitespace.
How can I count words in a text file using C++?
You can read the file word by word using the `>>` operator in a loop and increment a counter for each word successfully read until the end of the file is reached.
Are the solutions provided for both reading and writing files?
This specific set of solutions focuses on reading from text files, covering tasks like counting words and characters within existing files.
Content reviewed by the NCERT Help team. Editorial Team and update policy
NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.