You are on page 1of 12

EXPERIMENT NUMBER –Practical 10.

NAME – VADADA JATIN UID- 20BCS2424

SECTION - 25-C SUBJECT- C++ Lab

SEM-2nd

TOPIC OF EXPERIMENT –

WAP to copy the contents of one file to another and display it on output
screen.

FLOWCHART/ ALGORITHM -

1. First create all variables required in the program.

2. Ask the user to enter the total numbers of elements and store it in the variable count.

3. Allocate memory to the int pointer variable . The memory allocation is same as the
number of elements user will enter.

4. Use one for loop and read the element count.

5. Ask the user to enter the element and store it in arr (arr is working like an array here).

6. Also, increment the sum. Here, sum contains the total sum of all elements user will enter
.
7. After the loop is completed, we have the total sum saved in variable sum.
Print out this value to the user.
8. We have allocated the memory previously for the arr variable. Since the

program is completed, we don’t require this memory any longer. Release the
memory using free method and exit from the program.

PROGRAM CODE-

#include<iostream>

using namespace std;

int main()

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
OUTPUT:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING SUBJECT CODE-CSP- 152


USING C++ LAB
.

EXPERIMENT NUMBER – Practical 10.2

NAME – VADADA JATIN UID- 20BCS2424

SECTION - 25-C SUBJECT- C++ Lab

SEM-2nd

TOPIC OF 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-

1. Start of the program.


2. Enter name and age.

3. Show the name and age.


4. Show File will be created/error in creating file.
5. End of the program.

PROGRAM CODE-

#include <iostream>

#include <fstream>

using namespace std;

//class student to read and write student details class student

{
private:

char name[30];

int age;

public:

void getData(void)

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;

//re open file in input mode and read


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
OUTPUT –

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 10


writing learning objective/ Outcome

2. Post Lab Quiz Result 5

3. Student engagement in Simulation/ 5


Performance/ Pre Lab Questions

4. Total Marks 20

You might also like