Jaypee University of Engineering and Technology, Guna
Department of Computer Science and Engineering
Object Oriented Programming (CS102)
Tutorial – 9
(File Handling)
1. What is the difference between opening a file with a constructor function and opening a file
with open() function? When is one method preferred over the other?
2. Write the statemens for following situations:
i. Create an object called fob for writing, and associate it with a file name DATA.
ii. Create an object fin for reading a file “my_file.txt”.
iii. Create a file stream that can open a binary file “file.dat” in read and write mode.
3. How many file objects would you need to create to manage the following situations?
(a) To process four files sequentially.
(b) To merge two sorted files into a third file. Explain.
4. Write a program that creates a text file “TEXT.txt”on the disk. Write text on this file. Read this
file and display the following information on the screen in two columns:
Number of lines
Number of words
Number of characters
Strings should be left-justified and numbers should be right-justified in a suitable field width.
5. Two file named ‘Source1’ and ‘Source2’ contain sorted list of integers. Write a program that
reads the contents of both the files and stores the merged list in sorted form in a new file
named ‘Target’.
6. Write a program in C++ to read the file and store the lines into an array.
7. Write a program in C++ to copy a file in another name.
8. Write a program in C++ to merge two files and write it in a new file.
9. Write a program in C++ to encrypt a text file.
10. Write statements using seekg() to achieve the following:
(a) To move the pointer by 40 positions forward from current position.
(b) To go to the end after an operation is over.
(c) To go backward by 35 bytes from the end.
(d) To go to byte number 20 in the file.