You are on page 1of 7

EXPERIMENT NUMBER –Practical 10.

STUDENT’S NAME – LOGA ASWIN R

STUDENT’S UID – 21BCS10573

CLASS AND GROUP – 109 B

SEMESTER – 2nd

AIM OF THE EXPERIMENT – LEARN HOW TO USE File HANDLING IN C ++

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;

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
    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


(Kindly jot down the compile time errors encountered)
-NA-

PROGRAMS’ EXPLANATION (in brief)

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.

In C++ files are mainly dealt by three ways:

 fstream: Use for both read and write form/to files

 Ifstream: use to read form/to files.

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
 ofstream:Use to write form/tofiles

OUTPUT

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
EXPERIMENT NUMBER –Practical 10.2

STUDENT’S NAME – LOGA ASWIN R

STUDENT’S UID – 21BCS10573

CLASS AND GROUP – 109 B

SEMESTER – 2nd

AIM OF THE EXPERIMENT – LEARN HOW TO USE File HANDLING IN C ++

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

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)

-NA-

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
PROGRAMS’ EXPLANATION (in brief)

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.

In C++ files are mainly dealt by three ways:

 fstream: Use for both read and write form/to files

 Ifstream: use to read form/to files.

 ofstream:Use to write form/tofiles

OUTPUT

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
LEARNING OUTCOMES
 Identify situations where computational methods would be useful.
 Approach the programming tasks using techniques learnt and write pseudo-code.
 Choose the right data representation formats based on the requirements of the problem.
 Use the comparisons and limitations of the various programming constructs and choose the
right one for the task.

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 Lab Questions

4. Total Marks 20

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152

You might also like