C++ Worksheet
C++ Worksheet
nd
SEM - 2
TOPIC OF EXPERIMENT –
WAP to copy the contents of one file to another and display it on
output screen.
FLOWCHART/ ALGORITHM -
PROGRAM CODE-
#include<iostream>
int main()
cin>>sourceFile;
fs = fopen(sourceFile, "r");
if(fs == NULL)
cout<<"\nError Occurred!";
return 0;
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);
}
fclose(fs);
fclose(ft);
cout<<endl;
return 0;
No
OUTPUT:
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
SUBJECT CODE-CSP-
USING C++ LAB
152
.
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-
#include <iostream>
#include <fstream>
class student
private:
char name[30];
int age;
public:
void getData(void)
void showData(void)
cout<<"Name:"<<name<<",Age:"<<age<<endl;
};
int main()
student s;
ofstream file;
file.open("aaa.txt",ios::out);
if(!file)
return 0;
//open file1
ifstream file1;
file1.open("aaa.txt",ios::in);
if(!file1){
return 0;
file1.read((char*)&s,sizeof(s));
s.showData();
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.