FACULTY OF COMPUTER SCIENCE AND ENGINEERING
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lab Duration: 3 hrs.
CS101L Introduction to Computing & AI Lab
Lab No: 10
Instructor: Ms. Aneeqa Ramzan
Marks: 10
Date: 01-12-2025
Task 1: File Read and Write
Write a program to demonstrate reading from and writing to a file in C++.
Requirements:
•
File Writing:
o Create a text file named example.txt.
o
•
Use ofstream to write any content to the file.
File Reading:
o
Use ifstream to read the contents of example.txt.
o
Display the content line by line on the console.
Expected Output:
Writing to file...
File written successfully.
Reading from file...
Hello, World!
This is a file handling example.
Task 2: Word Count in a File
Write a program to count the number of words in a text file.
Requirements:
•
Create a text file named text.txt with some content (e.g., a paragraph of text).
•
Use ifstream to read the file word by word.
•
Count and display the total number of words in the file with words on the console.
Expected Output:
C++ is a general-purpose programming language.
It is widely used in competitive programming.
Total words in the file: 13
Task 3: Find and Replace in a File
Write a program to find and replace a specific word in a text file.
Requirements:
•
Create a file named data.txt with some sample text.
•
Ask the user to input the word to find and the word to replace it with.
•
Use ifstream to read the file and ofstream to write with append the updated content to the same
file and updated content.
Expected Output:
Enter the word to find: language
Enter the word to replace with: tool
C++ is a powerful programming language.
C++ is a powerful programming tool.