Professional Documents
Culture Documents
Lab-13
For Students:
Roll No
Name
Class
Instructor
_________________________
Mr. Irfan Ullah
_____________________________________________________________________________1
39
Programming Fundamentals - Lab [COSC-1201]
13.1. OBJECTIVES
1. File Handling
a. Input / Output with Files
i. Text Files
Text files are the normal .txt files that you can easily create using Notepad or any simple text
editors. They take minimum effort to maintain, are easily readable, and provide least security
and takes bigger storage space.
We can perform four major operations on the file, either text or binary:
C++ provides the following classes to perform output and input of characters to/from files:
_____________________________________________________________________________1
40
Programming Fundamentals - Lab [COSC-1201]
To work with files, we will always include fstream header file in our
programs. The following program describes the basic functionality to
create a file, write into file, and read from a file.
In order to open a file with a stream object we use its member function
open:
Open(filename, mode);
Where file name is the name of the file, and mode is an optional
parameter.
If we want to open a file i.e., afile.txt, we can use the built in function
open() of the stream object. The syntax is:
1. ofstream outfile;
2. outfile.open("afile.txt");
For fstream, the default value is only applied if the function is called
without specifying any value for the mode parameter. If the function is
_____________________________________________________________________________1
41
Programming Fundamentals - Lab [COSC-1201]
called with any value in that parameter the default mode is overridden,
not combined.
int main()
{ char array[50];
fstream object;
object.open("newfile.txt",ios::in|ios::out|ios::app);
if(object.is_open())
{ object<<"I am new to learning file handling \n";
object.close();
cout<<"Success \n";
}
else
cout<<"File opening error";
object.open("newfile.txt");
ofstream file;
file.open("data.txt",ios::binary|ios::app);
if(!file){
cout<<"Error opeing file";}
else{
file.write((char*)&array, sizeof(array));
file.close();
}
ifstream file1;
file1.open("data.txt");
if(!file1)
{cout<<"File opening error";
file1.close();
}
while(!file1.eof())
{
file1.read((char*)&array, sizeof(array));
cout<<"File says"<<array<<endl;
}
_____________________________________________________________________________1
43
Programming Fundamentals - Lab [COSC-1201]
file1.close();
}
void addrecord()
{ cout<<"Enter name";
cin.getline(p.name,50);
cout<<"Enter Age";
cin>>p.age;
cout<<"Enter Salary";
cin>>p.salary;
}
_____________________________________________________________________________1
44
Programming Fundamentals - Lab [COSC-1201]
if(strcmp(A,p.name)==0)
{
cout<<" name is : "<<p.name<<endl;
cout<<"Age is: "<<p.age<<endl;
cout<<"Salary is: "<<p.salary<<endl;
}
}
}
int main()
{
ofstream wfile("person.txt", ios::binary|ios::app);
if(!wfile.is_open())
{cout<<"File not open";
wfile.close();
}
addrecord();
wfile.write((char *)&p,sizeof(p));
cout<<"Person added";
wfile.close();
char Arr[30];
cout<<"Enter person name you want to search";
cin.ignore();
cin.getline(Arr, 30);
showperson(Arr);
}
_____________________________________________________________________________1
45
Programming Fundamentals - Lab [COSC-1201]
Write a program that inputs upto 10 integer values in to a file, and then
displays them on screen.
_____________________________________________________________________________1
46
Programming Fundamentals - Lab [COSC-1201]
13.3.2. Program # 2
Write a program that inputs character from the user and appends it in
existing file. The input ends if the user enters a full stop.
13.3.3. Program # 3
Write a program that copies the contents of one file to another file.
_____________________________________________________________________________1
47
Programming Fundamentals - Lab [COSC-1201]
13.3.4. Program # 4
_____________________________________________________________________________1
48
Programming Fundamentals - Lab [COSC-1201]
_____________________________________________________________________________1
49
Programming Fundamentals - Lab [COSC-1201]