Professional Documents
Culture Documents
21BCS10573 - C++ 10
21BCS10573 - C++ 10
SEMESTER – 2nd
TOPIC OF THE EXPERIMENT: WAP to copy the contents of one file to another and display it on output
screen.
FLOWCHART/ ALGORITHM
Step 1: Start.
Step 2: Create two files inside the directory where you're saving your program or going to save your
C++ program of copy file (given below)
Step 3: Or save your C++ program (that copies the content of one file to another) in that folder
where the two files codes.txt and cracker.txt are created.
Step 4: Now we will define few standard libraries.
Step 5: According to our question we will use the concept of file handling.
Step 6: It will store data in storage device permanently.
Step 7: Now to display output, enter the name of source file ascodes.txt
Step 8: Enter the name of target file as cracker.txt and press ENTER key to copy the content of
source file to target file.
Step 9: You will get the output.
Step 10: End.
PROGRAM CODE
#include<iostream>
using namespace std;
int main()
{
cout<<"Nayanika Baweja"<<endl;
cout<<"UID: 20BCS5348"<<endl;
cout<<"\n\n"<<endl;
char ch, sourceFile[20], targetFile[20];
FILE *fs, *ft;
cout<<"Enter the Name of Source File: ";
cin>>sourceFile;
In this program the concept we used is file handling. So basically, File handling provides a
mechanism to store the output of the program and use to perform the functions. A stream is an
abstraction which represents on which input or output task is performed. It can be represented as
source or destination.
OUTPUT
SEMESTER – 2nd
TOPIC OF THE EXPERIMENT: WAP to read the class object of student info such as name, age and roll
no from the keyboard and to store them on a specified file using read() and write() functions. Again, the same
file is opened for reading and displaying the contents of the file on the screen.
FLOWCHART/ ALGORITHM
Step 1: Start
Step 2: Create files in your directory.
Step 3: Now we will define few standard libraries.
Step 4: We will define variables under data type like name and age.
Step 5: According to our question we will use the concept of file handling.
Step 6: It will store data in storage device permanently.
Step 7: After using file handling, in output we will enter our name and age.
Step 8: It will display on screen and also be saved in given files.
Step 9: Hence, you will get the output.
Step 10: End.
PROGRAM CODE
#include <iostream>
#include <fstream>
using namespace std;
class student
{
private:
char name[30];
int age;
public:
void getData(void)
{
cout<<"Enter name:"; cin.getline(name,30);
cout<<"Enter age:"; cin>>age;
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
}
void showData(void)
{
cout<<"Name:"<<name<<",Age:"<<age<<endl;
}
};
int main()
{
cout<<"Nayanika Baweja"<<endl;
cout<<"UID: 20BCS5348"<<endl;
cout<<"\n\n"<<endl;
student s;
ofstream file;
file.open("aaa.txt",ios::out);
if(!file)
{
cout<<"Error in creating file.."<<endl;
return 0;
}
cout<<"\nFile created successfully."<<endl;
s.getData();
file.write((char*)&s,sizeof(s));
file.close();
cout<<"\nFile saved and closed succesfully."<<endl;
ifstream file1;
file1.open("aaa.txt",ios::in);
if(!file1)
{
cout<<"Error in opening file..";
return 0;
}
file1.read((char*)&s,sizeof(s));
s.showData();
file1.close();
return 0;
}
-NA-
In this program the concept we used is file handling. So basically, File handling provides a
mechanism to store the output of the program and use to perform the functions. A stream is an
abstraction which represents on which input or output task is performed. It can be represented as
source or destination.
OUTPUT
4. Total Marks 20