0% found this document useful (0 votes)
7 views

Oop Lab12

Task 1 involves writing a program to count the number of lines in a text file. The program opens the file, reads each line using a while loop, and increments a counter variable. It then displays the total line count. Task 2 involves writing a program to count the total number of characters in a text file. The program opens the file and uses a while loop to read each character, incrementing a counter variable. It finally displays the total character count.

Uploaded by

Sara Adnan
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)
7 views

Oop Lab12

Task 1 involves writing a program to count the number of lines in a text file. The program opens the file, reads each line using a while loop, and increments a counter variable. It then displays the total line count. Task 2 involves writing a program to count the total number of characters in a text file. The program opens the file and uses a while loop to read each character, incrementing a counter variable. It finally displays the total character count.

Uploaded by

Sara Adnan
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/ 3

Department of Computing

Lab 12: File Processing in C++


Date: May 17, 2023
Instructor: Dr Shams Qazi
Lab Engineer: Nadeem khan
NAME: ZAINAB ATHAR
CLASS + SECTION : BESE 13 B
CMS ID: 405094
Task 1:

Write a C++ program that reads a text file and counts the number of lines present in it.
The program should open the file, read each line, and increment a counter for each line
encountered. Finally, the program should display the total count of lines in the file.

Code

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream file("lines.txt", ios::out);
file << "This is line 1" << endl
<< "This is line 2" << endl
<< "This is line 3" << endl
<< "This is line 4" << endl
<< "This is line 5" << endl;
file.close();
file.open("lines.txt", ios::in);
if (!file){
cout<<"Error opening file"<<endl;
return 1;
}
string str;
int count=0;
while (getline(file,str)){
count++;
}
cout<<"Number of lines in file are : "<< count<<endl;
file.close();

return 0;
}
Output

Task 2:

Create a C++ program that opens a text file and calculates the total number of
characters present in it. The program should read each character in the file and
increment a counter for each character encountered. Finally, the program should
display the total count of characters in the file.

Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream file("character.txt", ios::out);
file << "This is line 1" << endl
<< "This is line 2" << endl
<< "This is line 3" << endl
<< "This is line 4" << endl
<< "This is line 5" << endl;
file.close();

file.open("character.txt" ,ios::in);
if (!file){
cout<<"Error opening file"<<endl;
return 1;
}
char ch;
int count_char=0;
while (file.get(ch)){
count_char++;
}
cout<<"Characters in file are : "<< count_char<<endl;
file.close();
return 0;
}

You might also like