0% found this document useful (0 votes)
4 views35 pages

File Handling

The document provides an overview of file handling in C++ including file pointers, their manipulators, and error handling during file operations. It explains how to manipulate file pointers using seekg() and seekp() functions, and discusses the importance of checking for end-of-file conditions. Additionally, it includes quiz questions to test understanding of file operations and their expected outputs.

Uploaded by

flankerisback
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views35 pages

File Handling

The document provides an overview of file handling in C++ including file pointers, their manipulators, and error handling during file operations. It explains how to manipulate file pointers using seekg() and seekp() functions, and discusses the importance of checking for end-of-file conditions. Additionally, it includes quiz questions to test understanding of file operations and their expected outputs.

Uploaded by

flankerisback
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Object Oriented Programming

(24ECE2101)

Batch 2024, ECE

Teaching Team

Dr Deepti Prit Kaur Dr Meenu Garg


Group X Group Y
File Handling
FILE POINTERS AND THEIR MANIPULATORS

• The two types of file pointers are read pointer and the write
pointer.
• While the read pointer is called, the get pointer specifies a
location from where the current read operation is initiated.
• The write or the put pointer specifies a location from where the
current write operation is initiated.
• Once the read or write operation is completed, the appropriate
pointer is automatically advanced.
FILE POINTERS AND THEIR MANIPULATORS
Manipulating File Pointers
Specifying the Offset

• There is another variant of seekg() and seekp() functions. It


can also accept two arguments— the offset and the
reference position .
Specifying the Offset

The syntax for a two argument seekg() or seekp() is


• inpFile.seekg(10, ios::beg)— moves to 10 bytes starting from the
beginning of the file
• inpFile.seekg(10, ios::cur)— moves to 10 bytes starting from the
current position of the file
• inpFile.seekg(0, ios::end)— moves to the end of the file
• inpFile.seekg(0, ios::beg)— moves to the beginning of the file
• inpFile.seekg(0, ios::cur)— stays at the current location of the file
pointer
• inpFile.seekg(-10, ios::cur)— moves 10 bytes backward starting
from the current position of the file pointer
• inpFile.seekg(-10, ios::end)— moves 10 bytes backward from the
end of the file
read() and write() Functions

• The get() reads a single character from the associated stream


and the put() is used to write a single character.
• read() and write() functions take two arguments—the
address of the variable that has to be written and the size of
that variable in bytes. to the associated stream.
Guess the output?

9
Guess the output?

10
Guess the output?

If no file exists then output is 🡪

11
Guess the output?

12
Guess the output?

Explanation:
•"1234567890" has 10
characters.
•in.seekg(-3, ios::end) moves the
get pointer to the 7th character
(index 7 = '8').
•Then in.get(ch) reads the
character at that position.

13
Guess the output?

14
Specifying the Offset
get() and put() functions
• The get() reads a single
character from the
associated stream and
the put() is used to
write a single character
to the associated
stream.
• Hobby.txt must already
exits.
ERROR HANDLING DURING FILE OPERATIONS

• Open a non-existent file in read mode.


• Open a read-only in ios::out mode.
• Open a file with invalid name.
• Read beyond the end-of-file.
• Write more data in a file stored on disk when no sufficient
disk space is available.
• Manipulate data stored in an unopened file.
• No file is connected with the stream object.
• Media errors which may occur while reading or writing data
• Class ios has a member function called that records
information on the status of a file that is currently being
used.
ERROR HANDLING DURING FILE OPERATIONS
Test for errors

• When we try to open a file, it may open successfully. But this


may not always be true. For ex, if the user tries to open a
non-existent file in read-mode or tries to open a read-only
file in write mode, then the file operation definitely fails.
• Therefore, it is always recommended to test for errors by
using the operator ! with an instance of the ifstream,
ofstream, or fstream .
• The operator ! is overloaded and returns a non-zero value if
an error(s) is encountered and a zero otherwise.
• The statements given here open a file using the open() and
check if the file was opened successfully.
DETECTING THE END-OF-FILE

• In real-world programs, we may not know the number of


records stored in the file.
• We need to analyze whether we have reached the end of the
file or not. Therefore, in our file handling programs, we need
to continuously check for end-of-file condition to prevent
reading data further from the file.
• There are two ways to detect the end-of-file condition.
Quiz Time

How to get position n bytes forward in fileObject ?

a. fileObject.tellg( ios::cur, n );
b. fileObject.tellg( n, ios:cur );
c. fileObject.seekg( n, ios::cur );
d. fileObject.seekg( ios:cur, n );

Answer: c
fileObject.seekg( n, ios::cur );
Quiz Time

Which of the following options is in relevance to ios::trunc


mode?

a. If the file is opened for output operations and it


already existed, no action is taken.
b. If the file is opened for output operations and it
already existed, its previous content is deleted and
replaced by the new one.
c. If the file is opened for output operations and it
already existed, then a new copy is created.
d. None of above

Answer: b
Quiz Time

What is the purpose of the tellp() function?


A) Tells the position of the file pointer for input
B) Moves the file pointer to a specific location
C) Tells the current position of the put pointer (output)
D) Closes the file

Answer: c
Quiz Time

What will happen if the file "test.txt" does not exist when
outfile.open("test.txt") is called?

A) The program will crash


B) A runtime error will occur
C) A new file "test.txt" will be created
D) File will be opened in read-only mode

Answer: c
Quiz Time
Quiz Time

What is the output of this program in the “test.txt” file?

a. This is an apple
b. apple
c. sample
d. This is a sample

Answer: d
Quiz Time

What is the purpose of getline(infile, line) in the code?

A) Reads a single character


from the file
B) Reads an entire line from
the file into a string
C) Writes a line to the file
D) Checks if the file is open

Answer: b
Quiz Time

What will be the content of "numbers.txt" after execution?

A) 12345
B) 1 2 3 4 5
C) 1\n2\n3\n4\n5
D) 1, 2, 3, 4, 5

Answer: b
Quiz Time

What will be printed if "check.txt" does not exist?

A) Runtime error
B) File opened successfully
C) File does not exist
D) Nothing is printed

Answer: c
Quiz Time

30
Quiz Time

31
Check the output?
#include <fstream.h>
void main()
{
int marks[] = {98, 67, 89, 100, 45, 65, 51, 78, 12, 43};
int arr[10] = {0};
fstream FILE; // create object

FILE.open("temp", ios::out | ios::binary); // open file for writing


FILE.write((char *)marks, sizeof(marks)); // write data
FILE.close(); // close file

FILE.open("temp", ios::in | ios::binary); // open for reading


FILE.read((char *)arr, sizeof(marks)); // read data

cout << "\nMARKS OBTAINED : ";


for (int i = 0; i < 10; i++)
cout << "\t" << arr[i]; // display data
FILE.close(); // close file
}
32
Problem Statement

Write a C++ program to count the number of characters and the number of
lines in a text file.

📌 Requirements:
1.The program should prompt the user to enter the filename.
2.It should open the file in read mode.
3.If the file does not exist or cannot be opened, display an error message.
4.If the file opens successfully:
•Count the total number of characters in the file (including spaces and
newline characters).
•Count the number of lines (each line ends with a newline character
\n).
5.Display the total number of characters and lines in the file.
6.If the file is empty, display an appropriate message.

Assume that contents of the file are 🡪


Solution

34
Thanks

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy