0% found this document useful (0 votes)
25 views

C++ Worksheet

1. The document describes an experiment conducted by Prakrit Shukla to copy the contents of one file to another and display it on the output screen. 2. The flowchart includes opening both files, reading from the source file using a loop, writing to the target file, and closing both files. 3. A second experiment is described to read student details from the keyboard, write it to a file, reopen the file and display the contents on the screen.

Uploaded by

Mannu Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

C++ Worksheet

1. The document describes an experiment conducted by Prakrit Shukla to copy the contents of one file to another and display it on the output screen. 2. The flowchart includes opening both files, reading from the source file using a loop, writing to the target file, and closing both files. 3. A second experiment is described to read student details from the keyboard, write it to a file, reopen the file and display the contents on the screen.

Uploaded by

Mannu Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

EXPERIMENT NUMBER –Practical 10.

NAME –PRAKRIT SHUKLA UID – 20BCS2336


SECTION - 25-C SUBJECT – C++ LAB

nd
SEM - 2

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
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
SUBJECT CODE-CSP-
USING C++ LAB
152
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-
USING C++ LAB
152
.

EXPERIMENT NUMBER – Practical 10.2

NAME – PRAKRIT SHUKLA UID- 20BCS2336


SECTION - 25-C SUBJECT- C++ Lab

nd
SEM-2

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 there 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