Professional Documents
Culture Documents
Lab Report 11
Theory:
1. File Handling:
All the programs we have looked at so far use input only from the keyboard, and output only
to the screen
Data that is held in variables is temporary and is lost when the program execution is finished
In order to store data permanently, we need to store it in files on the hard disk
2. Stream:
A transfer of information in the form of sequence of bytes.
Input stream:
A stream that flows from an input device to main memory.
Output stream:
A stream that flows from the main memory to an output device
<fstream. h>
Creating Streams:
ifstream in_stream;
ofstream out_stream;
Having created a stream, we can connect it to a file using the member function
open()
instream.open(“myfile.txt”)
outstream.open(“myfile.txt”)
it can also delete files that are previously formed
Creating and connecting streams to files in a document:
ifstream in_stream(“myfile.txt”)
ofstream out_stream(“myfile.txt”)
Disconnecting Streams from Files:
It is used to disconnect a stream from a file, we can use the member function
close ()
End of function:
3. Pointers:
A pointer is a variable that stores the memory address as its value.
A pointer variable points to a data type (like intor string) of the same type, and is created
with the *operator. The address of the variable you're working with is assigned to the
pointers.
Create a pointer variable with the name ptr at points to a string variable, by using the
asterisk sign * (string* ptr). Note that the type of the pointer has to match the type of the
variable you're working with.
Use the & operator to store the memory address of the variable and assign it to the pointer.
Lab work:
Program No 1:
#include<iostream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
int a[5]={1,2,3,4,5};
int *aptr;
aptr=a;
for(int i=0;i<=4;i++)
{
cout<<aptr<<"\t"<<*aptr<<endl;
aptr++;
}
}
Program No 2:
#include<iostream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
int a[6]={2,3,4,5,6,7};
int aa=5;
int *aptr,*bptr;
bptr=&aa;
aptr=a;
cout<<bptr<<endl<<endl;
cout<<*bptr<<endl<<endl;
cout<<aptr<<endl;
cout<<*++aptr;
}
Program no 3:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
int a=5,b=6,c=7;
int *aptr,*bptr,*cptr;
aptr=&a;
bptr=&b;
cptr=&c;
cout<<aptr<<endl<<endl;
cout<<*aptr<<endl<<endl;
cout<<bptr<<endl<<endl;
cout<<*bptr<<endl<<endl;
cout<<cptr<<endl<<endl;
cout<<*cptr<<endl<<endl;
}
Program no 4:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
int a=5;
int *aptr;
aptr=&a;
cout<<aptr<<endl<<endl;
cout<<*aptr<<endl<<endl;
}
Program No 5:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
Program No 6:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
ifstream input("Uff_My_God.txt");
string a;
while(!input.eof())
{
getline(input,a); cout<<a;
}
return 0;
}
Lab Task:
Program No 1:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
Program No 2:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
Program No 3:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
int a[5]={1,2,3,4,5};
int aa=1;
int bb=2;
int cc=3;
int dd=4;
int ee=5;
int *aptr,*bptr,*cptr,*dptr,*eptr;
aptr=a;
cout<<"first memeber = "<<aa<<endl;
cout<<"second memeber = "<<bb<<endl;
cout<<"third memeber = "<<cc<<endl;
cout<<"forth memeber = "<<dd<<endl;
cout<<"last memeber = "<<ee<<endl;
return 0;
}
int main()
cout<<"CMS ID 428302"<<endl;
int a[5]={1,2,3,4,5};
int *aptr;
aptr=a;
cout<<*aptr++<<endl;
return 0;