3.3 Oops

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

EXPERIMENT NUMBER – 3.

STUDENT’S NAME – Navneet STUDENT’S UID-21BCS6692


UID

CLASS AND GROUP –AIML 104 A SEMESTER -2nd

AIM OF THE EXPERIMENT:


EXPERIMENT:–

 Learn how to use file handling in c ++.

FLOWCHART/ ALGORITHM
ALGORITHM-
STEP 1: START
STEP 2: Declare Header file and namespace
STEP 3:In main method declare ch, sourcefile[20],targetfile[20] STEP 4: Declare
File*fs,*ft
STEP5 :Input the name of source file and pass through open(…)method and
store in fs
STEP 6: if fs =NULL print error ooccurred
STEP 7: Input the name of target file and pass through open(…)method and
store in ft STEP 8:ch= fget(fs)
STEP 9:if(ch!=EOF)
STEP10:fput(ch,ft)
STEP 11: ch=fget(fs) goto
STEP 12: print “File copied
Successfully
STEP 13: fclose(fs)
STEP 14: fclose(ft)
STEP 15: STOP

PROGRAM CODE

1)

#include <iostream>
#include <fstream>
using namespace std;
int main()
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103
{
int i; char name[10]; float sal;
cout<<"Name = Navneet UID = 21BCS6692<<endl;
ofstream outfile ("Employee.txt");
for (i=0;i<3;i++)
{
cout<<"Enter the name and salary of employee"<<endl;
cin>>name>>sal;
outfile<<name<<" "<< sal;
}
outfile.close();
ifstream inpfile("Employee.txt");
for (i=0; i<3; i++)
{
inpfile>>name;

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103


inpfile>>sal;
cout<<"Employee"<<i+1<<endl;
cout<<name<<endl<<" "<<sal<<endl;
}
inpfile.close();
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


 No Errors Found

PROGRAMS’ EXPLANATION (in brief)

Create two files inside the directory where you're saving your program or going to save
your C++ program of copy file (given below)
below).

Or save your C++ program (that copie


copiess the content of one file to another) in that folder
where the two files codes.txt and cracker.txt are created.

OUTPUT

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103


PROGRAM CODE
2)

#include <iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"Name = Navneet UID = 21BCS6692"<<endl;
ofstream fout;
string line;
fout.open("Sample.txt");
while(fout)
{
getline(cin,line);
if(line=="-1")
break;
fout<<line<<endl;
}
fout.close();
fstream fin;
fin.open("Sample.txt");
while(fin)
{
getline(fin,line);
cout<<line<<endl;
}
fin.close();
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


 No Errors Found

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103


PROGRAMS’ EXPLANATION (in brief)

OUTPUT

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103


PROGRAM CODE

3)

#include<iostream>
#include <fstream>
using namespace std;
int main()
{
cout<<"Name = Navneet UID = 21BCS6692"<<endl;
ofstream outfile;
outfile.open("Questions.txt");
outfile<<"What
What is your name"<<endl;
outfile<<"What is your Address"<<endl;
outfile.close();
outfile.open("Answers.txt");
outfile<<"\n Anil"<<endl;
outfile<<"\n Chandigarh"<<endl;
outfile.close();
char Quest[82],ans[50];
ifstream inpfile;
inpfile.open("Questions.txt"
inpfile.open("Questions.txt");
while(inpfile.eof()==0)
{
inpfile.getline(Quest,82);
cout<<Quest;
}
inpfile.close();
inpfile.open("Answers.txt");
while(inpfile.eof()==0)
{
inpfile.getline(ans,50);
cout<<ans;
inpfile.close();
}
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


 No Errors Found

PROGRAMS’ EXPLANATION (in brief)

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103


OUTPUT

PROGRAM CODE

4)

#include <iostream>
#include <fstream>
using namespace std;
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103
int main()
{

char quest[80],ans[50];
cout<<"Name = Navneet UID = 21BCS6692"<<endl;
ifstream inpfile1,inpfile2;
inpfile1.open("Questions.txt");
inpfile2.open("Answers.txt");
while (inpfile1.eof()==0|| inpfile2.eof()==0)
{
inpfile1.getline(quest,80);
cout<<"\n"<<quest;
inpfile2.getline(ans,20);
cout<<"\e"<<ans;
inpfile1.close();
inpfile2.close();

}
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


 No Errors Found

PROGRAMS’ EXPLANATION (in brief)

OUTPUT

PROGRAM CODE
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103
5)

#include<iostream>
using namespace std;
int main()
{
cout<<"Name = Navneet UID = 21BCS6692"<<endl;
char ch, sourceFile[20], targetFile[20];
FILE *fs, *ft;
cout<<"Enter the Name of Source File: ";
cin>>sourceFile;
fs = fopen(sourceFile, "r");
if(fs == NULL)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"\nEnter the Name of Target File: ";
cin>>targetFile;
ft = fopen(targetFile, "w");
if(ft == NULL)
{
cout<<"\nError Occurred!";
return 0;
}
ch = fgetc(fs);
while(ch != EOF)
{
fputc(ch, ft);
ch = fgetc(fs);
}
cout<<"\nFile copied successfully.";
fclose(fs);
fclose(ft);
cout<<endl;
return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


 No Errors Found

PROGRAMS’ EXPLANATION (in brief)

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103


OUTPUT

PROGRAM CODE
6)
#include <iostream>
#include <fstream>
using namespace std;
//class student to read and write student details
class student
{
private:
char name[30];

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103


int age;
public:
void getData(void)
{
cout<<"Name = Navneet UID = 21BCS6692"<<endl;
cout<<"Enter name:"; cin.getline(name,30);
cout<<"Enter age:"; cin>>age;

}
void showData(void)
{
cout<<"Name:"<<name<<",Age:"<<age<<endl;
}
};
int main()
{
student s;
ofstream file;

//open file in write mode


file.open("aaa.txt",ios::out);
if(!file)
{
cout<<"Error in creating file.."<<endl;
return 0;
}
cout<<"\nFile created successfully."<<endl;
//write into file
s.getData(); //read from user
file.write((char*)&s,sizeof(s)); //write into file
file.close(); //close the file

cout<<"\nFile saved and closed succesfully."<<endl;


SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103
//re open file in input mode and rread data
//open file1
ifstream file1;
//again open file in read mode
file1.open("aaa.txt",ios::in);
if(!file1){
cout<<"Error in opening file..";
return 0;
}
//read data from file
file1.read((char*)&s,sizeof(s));
//display data on monitor
s.showData();
//close the file
file1.close();
return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


 No Errors Found

PROGRAMS’ EXPLANATION (in brief)

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103


OUTPUT

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103


LEARNING OUTCOMES

 Understand the concepts of object


object-oriented programming including programming process
and compilation process.

 Apply different techniques to decompose a problem and programmed a solution with its
sub modules.

 Analyze and explain the behavior of simple programs involving the programming
addressed in the course.

 Implement and evaluate the programs using the syntax and semantics of object-oriented
object
programming.

 Design the solution of real--world problems in order to determine that the program
performs as expected.

EVALUATION COLUMN (To be filled by concerned faculty only)


Sr. No. Parameters Maximum Marks
Marks Obtained
1. Worksheet Completion including writing 10
learning objective/ Outcome
2. Post-Lab Quiz Result 5

3. Student engagement in Simulation/ 5


Performance/ Pre
Pre-Lab Questions
4. Total Marks 20

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103

You might also like