You are on page 1of 14

Fundamentals of Programming

Lab Report 11

Introduction to File Handling and Pointers


1st SEMESTER

Submitted to: Engr. Ali Hassan


Session: ME-14 Section: B Group: _
SUBMITTED BY
Name CMS Objective Lab Lab
Theory
(1) Work(3) Task Total
(3)
(3)
Malik Haseeb Ullah 428302

School of Mechanical and Manufacturing


Engineering
Objectives:
 Introduction to File Handling
 Introduction to Pointers

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

Header file included:

<fstream. h>

Creating Streams:

• Before we can use an input or output stream in a program, we must "create" it

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:

The eof ( ) function returns true if the end-of-file is reached


This function can be used in a loop to determine how many times the get() function should
be called to read the complete input file.

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++;
}
}

Figure 1 Output of program no 1

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;
}

Figure 2 Output of program no 2

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;
}

Figure 3 Output of program no 3

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;
}

Figure 4 Output of Program no 4

Program No 5:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;

ofstream newfile("DBZ.txt"); //output to file


int a,b,sum;
cin>>a;
cin>>b;
sum=a+b;
cout<<"Your Answer is "<<sum<<endl;
newfile<<"Your Answer is "<<sum<<endl;
return 0;
}
Figure 5 Output of Program no 5

Figure 6 New file

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;
}

Figure 7 my file that I include

Figure 8 Output of Program No 6

Lab Task:
Program No 1:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;

ifstream inp ("Uff_My_God.txt");


ofstream out ("Yah_Found_you.txt"); string
a; while (!inp.eof())
{
getline(inp,a);
out<<a<<endl; }
return 0;
}

Figure 9 First File

Figure 10 Copied File

Program No 2:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;

ofstream out ("All_info.txt",ios::app);


char repeat; do
{
cout<<"Please enter your Name: ";
out<<"Name: "; string name;
cin.ignore();
getline(cin,name);
out<<name<<endl;
cout<<"Please enter your CMS ID: ";
out<<"CMS ID: ";
int cms;
cin>>cms; out<<cms<<endl;
cout<<"Please enter your expected GPA: ";
out<<"Expected GPA: ";
float gpa;
cin>>gpa;
out<<gpa<<endl;
cout<<"Please enter your expected grade in FOP: ";
out<<"Expected grade in FOP: ";
char grade;
cin>>grade;
out<<grade<<endl;
cout <<"Wanna submit another form? YES/NO ";
out<<"Please Proceed "<<endl;
cin>>repeat;
}
while(repeat=='Y');
return 0;
}
Figure 11 Output of Program No 2

Figure 12 Output File Formed

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;
}

Figure 13 Output without loop


#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 !=5; i++)

cout<<*aptr++<<endl;

return 0;

Figure 14 Output with loop

You might also like