You are on page 1of 5

Lab 4

This manual covers:


i.

Concept of files in C++

Exercise 4.1 -----------------------------------------------------------------Write, compile, build and run the following program. The program will create a file and
check whether file is created successfully or not and then close the file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file; //object of fstream class
//opening file "sample.txt" in out(write) mode
file.open("sample.txt",ios::out);
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
cout<<"File created successfully.";
//closing the file
file.close();
return 0;
}
Exercise 4.2
Write, compile, build and run the following program. The program creates a file and
then write some text into then file, after writing text file will be closed and again file
will open in read mode, read all written text.
1

//C++ program to write and read text in/from file.


#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file; //object of fstream class
//opening file "sample.txt" in out(write) mode
file.open("sample.txt",ios::out);
if(!file)
{
cout<<"Error in creating file!!!"<<endl;
return 0;
}
cout<<"File created successfully."<<endl;
//write text into file
file<<"ABCD.";
//closing the file
file.close();
//again open file in read mode
file.open("sample.txt",ios::in);
if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return 0;
}
//read untill end of file is not found.
char ch; //to read single character
cout<<"File content: ";
while(!file.eof())
{
file>>ch; //read single character from file
cout<<ch;
}
file.close(); //close file

return 0;
}
Exercise 4.3
Write, compile, build and run the following program. The program will write and read
variables values in/from text file. Here you will learn how to write values in file and how
to access them
//C++ program to write and read values using variables in/from
file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char name[30];
int age;
fstream file;
file.open("aaa.txt",ios::out);
if(!file)
{
cout<<"Error in creating file.."<<endl;
return 0;
}
cout<<"\nFile created successfully."<<endl;
//read values from kb
cout<<"Enter your name: ";
cin.getline(name,30);
cout<<"Enter age: ";
cin>>age;
//write into file
file<<name<<" "<<age<<endl;
file.close();
cout<<"\nFile saved and closed succesfully."<<endl;
//re open file in input mode and read data
//open file
file.open("aaa.txt",ios::in);
if(!file){
cout<<"Error in opening file..";
return 0;
3

}
file>>name;
file>>age;
cout<<"Name: "<<name<<",Age:"<<age<<endl;
return 0;
}

Exercise 4.4
Write, compile, build and run the following program. The program program to
demonstrate example of tellg and tellp tellg() and tellp() function example in c++
programming language.
//C++ program to demonstrate example of tellg() and tellp()
function.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file;
//open file sample.txt in and Write mode
file.open("sample.txt",ios::out);
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
//write A to Z
file<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//print the position
cout<<"Current position is: "<<file.tellp()<<endl;
file.close();
//again open file in read mode
file.open("sample.txt",ios::in);
if(!file)
{
cout<<"Error in opening file!!!";
return 0;
}

cout<<"After opening file position is:


"<<file.tellg()<<endl;
//read characters untill end of file is not found
char ch;
while(!file.eof())
{
cout<<"At position : "<<file.tellg();
//current
position
file>>ch;
//read character from file
cout<<" Character \""<<ch<<"\""<<endl;
}
//close the file
file.close();
return 0;
}
Exercise 4.5
Write a C++ program to write number 1 to 100 in a data file NOTES.TXT.

Assignment 2:
Assuming that a text file named FIRST.TXT contains some text written into it, write a
function named vowelwords(), that reads the file FIRST.TXT and creates a new file
named SECOND.TXT, to contain only those words from the file FIRST.TXT which start
with a lowercase vowel (i.e., with 'a', 'e', 'i', 'o', 'u').

For example, if the file FIRST.TXT contains: Carry umbrella and overcoat when it rains
Then the file SECOND.TXT shall contain umbrella and overcoat it

You might also like